Ho在通过gridExtra中的arrange.grid创建的图表周围添加一个边框,其中包含一组ggplot2散点图 [英] Ho to add a border around a chart created via arrange.grid in gridExtra encompassing a set ggplot2 scatter plots

查看:1156
本文介绍了Ho在通过gridExtra中的arrange.grid创建的图表周围添加一个边框,其中包含一组ggplot2散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码:

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

#生成单独的图表
chrts_list_scts< - list()

#数据
数据(mtcars)

#A
chrts_list_scts $ a< - ggplot(mtcars)+
geom_point(size = 2,aes(x = mpg,y = disp,
color = as。因子(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,
color = as.factor(cyl))+
geom_smooth(aes(x = mpg,y = drat),
method =auto)+
xlab (MPG)+
ylab(Drat)+
theme_bw()+
主题(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,
color = as.factor(cyl))+
geom_smooth(aes(x = mpg,y = qsec),
method =auto)+
xlab(MPG)+
ylab(QSEC)+
guides(color = guide_legend(title =cyl))+
theme_bw()+
theme panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position =bottom,
legend.key = element_rect(color = NA) )

#排列网格
png(文件名=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,
顶部=
textGrob(label = title_text,
gp = gpar(
fontsize = 14,
font = 2)))
dev.off()
rm(title_text)

生成以下图表:



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



,但我不清楚如何达成一个可行的解决方案。



方面要求 h2>

除了生成边框,我还想:


  1. 能够对边界美学进行一些控制,比如改变边框的大小和颜色。

  2. 理想情况下,我想将这个解决方案封装在 arrange.grid 呼叫。因此,对象 chrts_list_scts $ all_scts 包含所有元素,包括图表和整齐的边框。






如果存在与剩余两个匹配的建议解决方案,我将很乐意接受仅解决边界方面主要要求的解决方案指出它会更好。

使用虹膜示例(但进一步简化)从问题中提供的链接添加最后一行。修改 gpar(...)组件(可能还有 width height code>)来获得不同的美学效果。 (这不是封装在 grid.arrange 调用中。)

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


#下一行添加边框
grid.rect(width = .98,height = .98 ,gp = gpar(lwd = 2,col =blue,fill = NA))

继续阴谋)





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

  grid.newpage()
ga < - arrangeGrob( (高度= .98,宽度= .98,gp = gpar(lwd = 2,col =蓝色,填充= NA))#边界,无填充
gt< - gTree(children = gList(ga,gb))
grid.draw(gt)


I'm using the code below:

# 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)

To generate the following chart:

I'm interested in adding border around that chart, as in the picture below:

Attempts

I tried to address this request via adding polygonGrob in the code:

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)))

but this generates a pointless chart with one line across in the bottom. I had a look at the seeming similar discussion on SO but it wasn't clear to me how to arrive at a working solution.

Side requirements

In addition to generating the border, I would like to:

  1. Be able to exercise some control over the border aesthetics, like changing size and colour of the border.
  2. Ideally, I would like to encapsulate this solution within the arrange.grid call. So at the object chrts_list_scts$all_scts has all elements including charts and neat border around all of them.


I will be happy to accept solutions that address the major requirements with respect to the border only, if there is a suggested solution that matches the remaining two points it will be even nicer.

解决方案

1) Using the iris example (but further simplified) from the link provided in the question just add the last line. Modify the gpar(...) components (and possibly the width and height) to get different aesthetics. (This is not encapsulated in the grid.arrange call.)

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))

(continued after plot)

2) This is a variation of solution (1) in which on the plus side encapsulates both the graphics and border in the gt gTree by creating grobs to hold each. On the other hand it does involve some additional complexity:

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)

这篇关于Ho在通过gridExtra中的arrange.grid创建的图表周围添加一个边框,其中包含一组ggplot2散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆