gpt4 book ai didi

r - 如何在包含一组ggplot2散点图的gridExtra中通过arrange.grid创建的图表周围添加边框

转载 作者:行者123 更新时间:2023-12-02 03:33:33 27 4
gpt4 key购买 nike

我使用下面的代码:

# Libs
require(ggplot2); require(gridExtra); require(grid)

# Generate separate charts
chrts_list_scts <- list()

# Data
data("mtcars")

# A
chrts_list_scts$a <- ggplot(mtcars) +
geom_point(size = 2, aes(x = mpg, y = disp,
colour = as.factor(cyl))) +
geom_smooth(aes(x = mpg, y = disp),
method = "auto") +
xlab("MPG") +
ylab("Disp") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none")

# B
chrts_list_scts$b <- ggplot(mtcars) +
geom_point(size = 2, aes(x = mpg, y = drat,
colour = as.factor(cyl))) +
geom_smooth(aes(x = mpg, y = drat),
method = "auto") +
xlab("MPG") +
ylab("Drat") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none")

# C
chrts_list_scts$c <- ggplot(mtcars) +
geom_point(size = 2, aes(x = mpg, y = qsec,
colour = as.factor(cyl))) +
geom_smooth(aes(x = mpg, y = qsec),
method = "auto") +
xlab("MPG") +
ylab("QSEC") +
guides(colour = guide_legend(title = "cyl")) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "bottom",
legend.key = element_rect(colour = NA))

# Arrange grid
png(filename = "chrts.PNG", width = 6,
height = 10, units = 'in', res = 300)
title_text <- c("mtcars")
chrts_list_scts$all_scts <- grid.arrange(chrts_list_scts$a,
chrts_list_scts$b,
chrts_list_scts$c,
top =
textGrob(label = title_text,
gp = gpar(
fontsize = 14,
font = 2)))
dev.off()
rm(title_text)

生成以下图表:

first chart

我有兴趣在该图表周围添加边框,如下图所示:

with border

尝试

我尝试通过在代码中添加 polygonGrob 来解决此请求:

chrts_list_scts$all_scts <- grid.arrange(chrts_list_scts$dep_work,
chrts_list_scts$chld_work,
chrts_list_scts$pens,
polygonGrob(x = c(0,0.5,1.05),
y = c(0,0.5,1.05)
),
top =
textGrob(label = title_text,
gp = gpar(
fontsize = 14,
font = 2)))

但这会生成一个毫无意义的图表,底部有一条横线。我看了一下看似相似的discussion on SO但我不清楚如何找到可行的解决方案。

附带要求

除了生成边框之外,我还想:

  1. 能够对边框美观进行一些控制,例如更改边框的大小和颜色。
  2. 理想情况下,我希望将此解决方案封装在 arrange.grid 调用中。因此,chrts_list_scts$all_scts 对象包含所有元素,包括图表和所有元素周围整齐的边框。
<小时/>

我很乐意接受仅解决与边界有关的主要要求的解决方案,如果有一个与其余两点相匹配的建议解决方案,那就更好了。

最佳答案

1) 使用问题中提供的链接中的 iris 示例(但进一步简化),只需添加最后一行。修改gpar(...)组件(可能还有宽度高度)以获得不同的美感。 (这没有封装在grid.arrange调用中。)

library(ggplot2)
library(grid)
library(gridExtra)

g <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
grid.arrange(g, g, ncol=2)


# next line adds border
grid.rect(width = .98, height = .98, gp = gpar(lwd = 2, col = "blue", fill = NA))

(情节后继续)

screenshot

2) 这是解决方案 (1) 的变体,其中的优点是通过创建 grobs 来保存每个图形和边框,从而将图形和边框封装在 gt gTree 中。另一方面,它确实涉及一些额外的复杂性:

grid.newpage()
ga <- arrangeGrob(g, g, ncol = 2)
gb <- rectGrob(height = .98, width = .98, gp = gpar(lwd = 2, col = "blue", fill = NA)) # border, no fill
gt <- gTree(children = gList(ga, gb))
grid.draw(gt)

关于r - 如何在包含一组ggplot2散点图的gridExtra中通过arrange.grid创建的图表周围添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33920371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com