【问题标题】:Move the legend item itself in ggplot2在 ggplot2 中移动图例项本身
【发布时间】:2021-02-28 23:48:59
【问题描述】:

我有以下情节,我可以在guide 中移动带有label.vjust 的图例标签,但这只会移动标签而不是项目本身。

这是我的情节代码:

b <- ggplot(data = subset(sub.girders.plot, Sample == "CSI"), 
   aes(x = Girder, y = `Moment LLDF`, fill = factor(S,levels = c("9","12","15")))) +  theme_classic() +
    geom_boxplot(outlier.shape = NA, position = position_dodge(width = 0.75),
                 fatten = 1, lwd = 0.2) +
    stat_summary(fun = mean, geom="point", shape=23, size=0.5, stroke = 0.2,
                 position = position_dodge(width = 0.75)) + 
    stat_boxplot(geom ='errorbar', linetype=1, width=0.5, lwd = 0.2, 
                 position = position_dodge(width = 0.75)) +
    geom_point(data = subset(sub.girders.plot, Sample == "Rigid Rotation Method"), 
               aes(shape = Sample), stroke = 0.3, size = 2, 
               position = position_dodge(width = 0.75)) +
  geom_vline(xintercept = 1.5, col='gray', lwd=0.2) +
  geom_vline(xintercept = 2.5, col='gray', lwd=0.2) +
  geom_vline(xintercept = 3.5, col='gray', lwd=0.2) +
  geom_vline(xintercept = 4.5, col='gray', lwd=0.2) +
  annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf, size = 0.35)+
  annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf, size = 0.35) +
  annotate("segment", x=Inf, xend=Inf, y=-Inf, yend=Inf, size = 0.35) +
  scale_shape_manual(values = c(8)) +
      labs(x = element_blank(), shape = element_blank(), 
           fill = "Analytical Data with \n Girder Spacing (ft) of:") +
    theme(legend.title.align = 0.5, legend.position = "bottom", 
          axis.text.y = element_text(size=8, colour="black"),
          axis.text.x = element_text(size=8, colour="black", angle=40, vjust=0.6),
          axis.title.y = element_text(size=8, colour="black", vjust=0.5, margin = margin(0,4,0,-6)),
          axis.line=element_blank(), 
          axis.ticks = element_line(colour = "black", size = 0.2),
          legend.text = element_text(size=8, margin = margin(0,5,0,0)),
          legend.title = element_text(size=8, margin = margin(0,2,0,0)),
          strip.text.x = element_text(size = 8), axis.ticks.x=element_blank(),
          strip.background = element_rect(color = "black", size = 0.4),
          legend.spacing.x = unit(0.1, 'cm'), legend.spacing.y = unit(0.1, 'cm'),
          legend.background = element_blank(), legend.box.background = element_rect(color = "black", size = 0.2)) +
  guides(fill = guide_legend(label.position = "bottom", label.hjust = 0.8, title.vjust = 0.8),
         shape = guide_legend(keywidth = unit(3, "mm"),keyheight = unit(3.5, "mm"), label.vjust = -0.5)) +
  facet_wrap(vars(UG), ncol = 2)

现在的情节是这样的:

我想对此图表进行两次编辑:

  1. 向下移动星标。
  2. 使我的箱线图图例项中的点更小(或从图例项中删除)。目前,当我在geom_point 中增大点大小时,图例中的这个点也会变大。

期望的输出:

编辑

我能够通过以下方式更改箱线图中 fill 中的点:

. . . + guides(fill = guide_legend(label.position = "bottom", label.hjust = 0.8, title.vjust = 0.8, 
                         override.aes = list(shape = c(23))),
     shape = guide_legend(keywidth = unit(3, "mm"), keyheight = unit(3, "mm"), 
                          label.vjust = -1))

我仍在寻找一种方法来使用标签将图例项向下移动

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    隐藏点应该很容易。

    将 show.legend= FALSE 添加到 geom_point。

    geom_point(aes(...), show.legend = FALSE) 编辑:

    其实你的点是来自stat_summary,所以把它加进去。

    【讨论】:

    • 这也删除了Rigid Rotation Method 的图例项。这不是我想要的。
    • “用 override.aes 控制 ggplot2 中的图例外观”aosmith.rbind.io/2020/07/09/ggplot2-override-aes
    • 该链接可让您调整大小。我还没有测试过,但也许调整闪避也可以修复位置?
    • label.vjust 是需要修改的
    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多