首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整和移动手动grob图例ggplot

调整和移动手动grob图例ggplot
EN

Stack Overflow用户
提问于 2020-04-19 06:20:55
回答 1查看 107关注 0票数 1

我有一个ggplot2对象,我试图为一些vlines添加一个图例。我遵循了以下内容:(ggplot2: manually add a legend)但是我无法得到我想要的输出。任何建议都是非常感谢的。

在那里我得到了:

我想要的输出是将手动图例直接放在物种图例的上方(或下方)

代码语言:javascript
复制
library(ggplot2)  
library(grid)  
library(gtable)
library(dplyr)

plot2 <- iris %>% 
  ggplot() + 
  geom_line(aes(Sepal.Length, Sepal.Width, color = Species)) + 
  facet_wrap(Species ~ ., nrow = 3) +
  geom_vline(xintercept = 6, y = 4.5, linetype = "dashed", color = "darkblue") +
  geom_vline(xintercept = 7, y = 4.5, linetype = "dashed", color = "black") 

L1 = linesGrob(x = unit(c(.5, .5), "npc"), y = unit(c(.25, .75), "npc"),
               gp = gpar(col = "black", lty = "longdash"))
L2 = linesGrob(x = unit(c(.5, .5), "npc"), y = unit(c(.25, .75), "npc"),
               gp = gpar(col = "darkblue", lty = "longdash"))
T1 = textGrob("line 1 explaing something", x = 0, just = "left")
T2 = textGrob("line 2 explaing something", x = 0, just = "left")

leg = gtable(width = unit(c(1,5), "cm"), height = unit(c(1,1,1,1), "cm"))
#leg = gtable_add_grob(leg, rectGrob(gp = gpar(fill = NA, col = "black")), t=2,l=1,b=4,r=2)

leg = gtable_add_grob(leg, L1, t=2, l=1)
leg = gtable_add_grob(leg, L2, t=3, l=1)
leg = gtable_add_grob(leg, T1, t=2, l=2)
leg = gtable_add_grob(leg, T2, t=3, l=2)

g = ggplotGrob(plot2)

pos = g$layout[grepl("panel", g$layout$name), c('t', 'l')]
g = gtable_add_cols(g, sum(leg$widths), pos$l[1])
g = gtable_add_grob(g, leg, t = pos$t[1], l = pos$l[1] + 1)
g = gtable_add_cols(g, unit(6, "pt"), pos$l[1])

# Draw it
grid.newpage()
grid.draw(g)

我也尝试过这种方法:

代码语言:javascript
复制
vlines <- data.frame(line = c("Line 1 Explaining Something", "Line 2 Explaining Something"), 
                     y = c(4.5, 4.5), x = c(6, 7))

ggplot() + 
  geom_line(data = iris, aes(Sepal.Length, Sepal.Width, color = Species)) + 
  facet_wrap(Species ~ ., nrow = 3) +
  geom_linerange(data = vlines, 
                 aes(x = x, 
                     ymax = y,
                     ymin = 0,
                     color = line),
             linetype = "dashed") +
  geom_linerange(data = vlines, 
                 aes(x = x, 
                     ymax = y,
                     ymin = 0,
                     color = line),
             linetype = "dashed") 

但正如你所看到的,它没有添加破折号,而是将它与另一种线型结合在一起……

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-19 08:24:39

实现所需输出的一种可能的解决方案是使用ggnewscale包中的new_scale_color函数为垂直线设置新的颜色图例。

在我的解决方案中,我还用geom_vline替换了geom_linerange,只需指定x截取值就可以绘制垂直线。

在您的示例中,您可以绘制类似这样的内容:

代码语言:javascript
复制
library(ggnewscale)
library(ggplot2)

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + 
  geom_line()+
  facet_wrap(Species ~ ., nrow = 3) +
  new_scale_color()+
  geom_vline(data = vlines, 
             aes(xintercept = x, color = line), linetype = "dashed")+
  scale_color_manual(values = c("blue","black"), name = "")

这是你要找的东西吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61297054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档