有没有办法将gTree转换回GGR中可行的绘图?

有没有办法将gTree转换回GGR中可行的绘图?,r,ggplot2,r-grid,R,Ggplot2,R Grid,以下是一些示例数据: exampledata <- structure(list(x = c(2.93131952459005, 3.21275054434318, 1.36466997175509, 2.13626543532502, 1.45889556823722, 1.94598707699052, 0.719062322132357, 2.38139571953234, 2.37813367615963, 3.98126576880209), y = c(7.51581380

以下是一些示例数据:

exampledata <- structure(list(x = c(2.93131952459005, 3.21275054434318, 1.36466997175509, 
2.13626543532502, 1.45889556823722, 1.94598707699052, 0.719062322132357, 
2.38139571953234, 2.37813367615963, 3.98126576880209), y = c(7.51581380181603, 
9.77495763943671, 8.9666894018554, 8.62675858853528, 7.89238665417542, 
9.84865061237773, 7.24526820962333, 7.64727218939944, 7.28026738945878, 
8.6913070524479), z = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L), .Label = c("a", "b", "c"), class = "factor"), z2 = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("cat", "dog"), class = "factor")), class = "data.frame", row.names = c(NA, 
-10L))

简而言之,答案是否定的。情节就像食谱。树就像食谱制作的蛋糕。你不能为了拿回食谱而解开蛋糕

但是,这里的答案是,您可以将绘图缝合在一起,然后修改图例,而不是修改图例,然后提取图例并将其缝合在一起。因此,如果您按以下顺序操作:


asdf简短的答案是否定的。ggplot就像一个配方。树就像食谱制作的蛋糕。你不能为了拿回食谱而解开蛋糕

但是,这里的答案是,您可以将绘图缝合在一起,然后修改图例,而不是修改图例,然后提取图例并将其缝合在一起。因此,如果您按以下顺序操作:


asdf不确定这是否有帮助@user63230-Hmm这是一篇非常有趣的帖子。但是,我认为它与我想要的正好相反(独立于图例更改打印点大小)。但这对我来说是新的,所以我可能错了。但是,我没有看到任何更改图例属性的选项。相关:您打算如何使用
绘图网格
?什么不适合你?我不确定这个问题是否说明问题很清楚。你似乎被困在了下一步,而你没有显示出来。@MrFlick我已经编辑了这个问题,希望能让它更清楚一点。不确定这是否有帮助@user63230-Hmm这是一篇非常有趣的文章。但是,我认为它与我想要的正好相反(独立于图例更改打印点大小)。但这对我来说是新的,所以我可能错了。但是,我没有看到任何更改图例属性的选项。相关:您打算如何使用
绘图网格
?什么不适合你?我不确定这个问题是否说明问题很清楚。看来你还停留在下一步,而你没有显示出来。@Flick先生,我对问题进行了编辑,希望能让问题更清楚一点。非常好,谢谢!我最终跳过了get_legend参数,只生成了两个图(每个图都有整个图例的一半),然后用plot_网格缝合在一起。但是这个答案非常有效!非常好,谢谢!我最终跳过了get_legend参数,只生成了两个图(每个图都有整个图例的一半),然后用plot_网格缝合在一起。但是这个答案非常有效!
asdf <- ggplot(exampledata, aes(x = x, y = y, color = z, shape = z)) +
  geom_point() + 
  geom_line(aes(color = z, linetype = z2))+
  scale_linetype_manual(values = c(1,2,3)) +
  theme(legend.position = 'top', 
  legend.spacing = unit(2, 'cm'))
grid::grid.ls(grid::grid.force())

# Set the size of the point in the legend to 2 mm
grid::grid.gedit("key-1-[-0-9]+-1.2-[-0-9]+-2-[-0-9]+", size = unit(4, "mm"))

# save the modified plot to an object
g2 <- grid::grid.grab()
ggsave(g2, filename = 'g2.tiff')
datasetb <- structure(list(x = c(2.55279478309192, 0.929375129220925, 1.56509894419863, 
2.48026699500513, 1.18018131012236, 1.79675395182943, 0.817046700547386, 
1.99710482619256, 2.18780091987683, 3.41661353718804), y = c(8.88460717718884, 
9.11053089978428, 7.68492406933585, 8.23110925234432, 7.48154953916593, 
9.0253526297593, 9.41899905471655, 8.54779428609509, 9.17050925351926, 
5.83078691211861)), class = "data.frame", row.names = c(NA, -10L
))

b <- ggplot(data = datasetb, aes(x = x, y = y) +
  geom_point()
prow <- plot_grid( asdf + theme(legend.position="none"),
                   b + theme(legend.position="none"),
                   align = 'vh',
                   labels = c("A", "B"),
                   hjust = -1,
                   nrow = 1
)

legend_a <- get_legend(asdf + theme(legend.position="top"))


p <- plot_grid( legend_a, prow, ncol = 1, rel_heights = c(.2, 1))

tiff("BothPlots.tiff", units = 'in', width = 12, height = 7, res = 400)

p
dev.off()