相关疑难解决方法(0)

grid.arrange 的背景

我制作了一个类似于下面代码所描述的图,从而生成了发布的图像。我不知道如何将整个背景设置为与定义子图时使用的相同的“grey80”颜色。IE。我想用相同的颜色为绘图之间和图例两侧的白色区域着色。这是否有可能实现,也许是通过一些奇特的 gridgrob-magic?

这个问题类似于更改 grid.arrange 输出的背景颜色,但如果可能的话,我想要一个不使用 png() 函数的解决方案

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

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width,
                       colour = Species)) + 
  ggtitle('Sepal') + 
  geom_point() + theme_bw() + 
  theme(rect = element_rect(fill = 'grey80'))


p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width,
                       colour = Species)) + 
  ggtitle('Petal') + 
  geom_point() + theme_bw() + 
  theme(rect = element_rect(fill = 'grey80'))

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = "bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 r-grid

6
推荐指数
1
解决办法
6285
查看次数

使用coord_fixed时如何在ggplot2中为整个背景着色

coord_fixed()与GGPLOT2使用,它不会似乎是可以设置整个情节的背景颜色。考虑以下简单示例:

library(ggplot2)
test_data <- data.frame(x=1:10)
test_data$y <- sqrt(test_data$x)
p1 <- ggplot(test_data) + geom_point(aes(x, y))
p1
Run Code Online (Sandbox Code Playgroud)

简单示例图

我可以轻松地使背景成为另一种颜色,例如扎眼的绿色:

p1 + theme(plot.background=element_rect(fill="green"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

但是,如果我想使用coord_fixed()并将背景颜色设置为绿色怎么办?当我尝试时,这ggplot2会在情节的底部和顶部留下一条白色的条:

p1 + theme(plot.background=element_rect(fill="green")) + coord_fixed()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如何完全填充上述图的背景(顶部和底部没有白色条纹)?我正在循环使用该animation包来制作多个子图,并且需要确保所有子图的背景是相同的(非白色)颜色,包括我需要使用的颜色coord_fixed()

r ggplot2

5
推荐指数
1
解决办法
579
查看次数

标签 统计

ggplot2 ×2

r ×2

r-grid ×1