ggplot中的grid.text在plot_grid

时间:2017-10-17 13:14:15

标签: r ggplot2 grid cowplot

我正在尝试使用plot_grid R包中的cowplot函数将两个图组合在一起。但是,我收到了以下错误:

  

开关出错(x [[2]] [[1]] $ name,C_abline = C_abline(x [[2]]),   C_plot_new = C_plot_new(x [[2]]),: EXPR必须是长度为1的向量

因此,测试两个图形我发现错误来自我使用grid_text的ggplot。因此,在我的例子中,我只包括一个情节。使用以下内容可以重现问题:

library(cowplot)
library(ggplot2)
library(ggforce)
library(grid)


### Example
circles <- as.data.frame(cbind(c(0.5, 1.5, 2.5), c(1, 2, 1), c(0.2, 0.2, 0.2)))

# Behold the some circles
ggplot() + geom_circle(aes(x0=V1, y0=V2, r=V3, fill=c("red", "blue", "green")), data=circles)+
  theme_bw() + ylab(expression(symbol('\253'))) + xlab(expression(symbol('\253')))+ theme(legend.position="none",
                                                                                          axis.title.x=element_text(size = 50),
                                                                                          axis.text.x=element_blank(),
                                                                                          axis.ticks.x=element_blank(),
                                                                                          axis.title.y=element_text(size = 50),
                                                                                          axis.text.y=element_blank(),
                                                                                          axis.ticks.y=element_blank()) 

grid.text("Distinct", x = unit(0.04, "npc"), y = unit(0.80, "npc"))

exP <- recordPlot()

plot_grid(exP)

我很高兴知道如何在这种对象上使用plot_grid(ggplot + grid_text)。

2 个答案:

答案 0 :(得分:2)

试试这个:

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


### Example
circles <- as.data.frame(cbind(c(0.5, 1.5, 2.5), c(1, 2, 1), c(0.2, 0.2, 0.2)))

# Behold the some circles
p1 <- ggplot() + 
  geom_circle(aes(x0=V1, y0=V2, r=V3, fill=c("red", "blue", "green")), data=circles)+
  theme_bw() + ylab(expression(symbol('\253'))) + xlab(expression(symbol('\253')))+ 
  theme(legend.position="none",
        axis.title.x=element_text(size = 50),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_text(size = 50),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()) 

print(p1)
grid.text("Distinct", x = unit(0.04, "npc"), y = unit(0.80, "npc"))
p2 <- grid.grab()

grid.arrange(p1,p2, nrow=1)

enter image description here

答案 1 :(得分:2)

你的例子应该有用,而事实上它不是我需要研究的错误。

然而,即使它确实有效,我也建议不要这样做。我没有很好的经验,有记录的情节,一般来说,首先绘制情节,然后捕捉它的整个往返似乎很尴尬,容易出错。我建议做你想做的三种选择。他们都给你相同的最终结果。

首先,您可以使用网格图形将ggplot和任何其他grob组合成一个组合的grob,然后您可以将其移交给plot_grid()

library(cowplot)
library(ggplot2)
library(ggforce)
library(grid)

# Plot of mpg data, to use in plot_grid below
pmpg <- ggplot(mpg, aes(x=cty, y=hwy)) + geom_point()

# Plot with circles
circles <- as.data.frame(cbind(c(0.5, 1.5, 2.5), c(1, 2, 1), c(0.2, 0.2, 0.2)))
pcircles <- ggplot() + 
  geom_circle(aes(x0=V1, y0=V2, r=V3, fill=c("red", "blue", "green")),
              data=circles) +
  theme_bw() + ylab(expression(symbol('\253'))) + 
  xlab(expression(symbol('\253'))) + 
  theme(legend.position="none",
        axis.title.x=element_text(size = 50),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_text(size = 50),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank()) 

# text grob
gtext <- grid::textGrob("Distinct", x = unit(0.04, "npc"), y = unit(0.80, "npc"))

# make combined grob
gcombined <- grid::grobTree(ggplotGrob(pcircles), gtext)

plot_grid(pmpg, gcombined)

结果:

enter image description here

其次,我编写了ggdraw()函数和相关函数,以便轻松组合ggplot对象和其他网格对象。例如,你可以将你的文本grob绘制到这样的图上:

pcombined <- ggdraw(pcircles) + draw_grob(gtext)
plot_grid(pmpg, gcombined)

结果与以前完全一样。

而且,如果你想要做的只是绘制一些文字,你也可以使用draw_text()而不是draw_grob()

pcombined <- ggdraw(pcircles) + draw_text("Distinct", x = 0.04, y = 0.8)
plot_grid(pmpg, gcombined)

最后,我想指出使用grid.grab()的{​​{3}}也适用于plot_grid()而不是grid.arrange()