【问题标题】:ggplot2 Delete elements from legendggplot2 从图例中删除元素
【发布时间】:2020-07-11 02:18:22
【问题描述】:

我有这个情节:

根据这些数据制作:

   days variable    value        sd
1    X1  Control 75.03424  3.857730
2    X2  Control 70.17851  2.913420
3    X3  Control 65.01627  9.188947
4    X4  Control 65.70995 10.882072
5    X5  Control 56.98791  8.070014
6    X6  Control 56.64376  4.827183
7    X1   Stress 75.63113  3.207749
8    X2   Stress 70.56030  5.626266
9    X3   Stress 61.56402  7.078610
10   X4   Stress 48.04541 15.287234
11   X5   Stress 43.54458  8.148382
12   X6   Stress 37.51121  9.494008

使用此代码:

significance <- data.frame(days=c("X4","X5","X6"),value=c(82,70,67), variable=NA)

# Plot
library(ggplot2)
library(extrafont)
library(scales)
library(Cairo)

ggplot(my_mean, aes(x=days, y=value, fill=variable)) +
  geom_bar(stat='identity', position='dodge', width = 0.75) +
  geom_errorbar(aes(ymin = value-sd, ymax = value+sd),
                position = position_dodge(0.75),
                width = 0.3) +
  labs(x='\nDAT',y='μg/cm2\n') +
  scale_y_continuous(limits = c(0,90), expand = c(0,0),
                     breaks = seq(from=0,to=90,by=10)) +
  scale_x_discrete(labels = c(0,7,14,21,27,35)) +
  ggtitle('Chlorophyll Content\n') +
  geom_point(data=significance, aes(y=value),shape='*',size=6) +
  scale_color_manual(values = c("Control" = 'gray45', "Stress" = 'gray')) +
  scale_fill_manual(values = c("Control" = 'gray45', "Stress" = 'gray')) +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
        panel.background = element_rect(fill = 'white'),
        plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
        axis.title = element_text(family = 'Calibri',face = 'bold'),
        axis.text = element_text(family = 'Calibri'),
        legend.text = element_text(family = 'Calibri',face = 'bold'),
        legend.position = c(0.92, 0.91),
        legend.key = element_rect(fill = NA,color = NA),
        legend.title = element_blank(),
        legend.background = element_blank()
        )

请注意我如何制作数据框来映射图中的重要点。但是不知道为什么,这些点也被添加到了传说中,不希望发生这种情况。我怀疑这可以通过theme 配置中的一些legend 函数来解决,但我无法找到要使用的函数以及它有哪些参数。

有什么建议吗?或者也许是一种更好的方法来绘制我的意义“星星”?

【问题讨论】:

  • 如果您不希望星星出现在图例中,请尝试geom_point(..., show.legend = F)。这是你的意思吗?
  • 嗨@PrayagGordy。是的,这是预期的输出,谢谢!
  • 很高兴我能帮上忙!我会添加它作为答案。

标签: r ggplot2 legend significance


【解决方案1】:

如果您不希望星星出现在图例中,请尝试geom_point(..., show.legend = F)。事实上,show.legend = F 是许多ggplot2 层中的一个选项。

【讨论】:

    【解决方案2】:

    你可以试试这个:

    ggplot(my_mean, aes(x=days, y=value, fill=variable)) +
      geom_bar(stat='identity', position='dodge', width = 0.75) +
      geom_errorbar(aes(ymin = value-sd, ymax = value+sd),
                    position = position_dodge(0.75),
                    width = 0.3,color='black') +
      labs(x='\nDAT',y='μg/cm2\n') +
      scale_y_continuous(limits = c(0,90), expand = c(0,0),
                         breaks = seq(from=0,to=90,by=10)) +
      scale_x_discrete(labels = c(0,7,14,21,27,35)) +
      ggtitle('Chlorophyll Content\n') +
      geom_point(data=significance, aes(y=value),shape='*',size=6,color='black') +
      scale_color_manual(values = c("Control" = 'gray45', "Stress" = 'gray')) +
      scale_fill_manual(values = c("Control" = 'gray45', "Stress" = 'gray')) +
      guides(fill=guide_legend(override.aes=list(shape=NA)))+
      theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
            panel.background = element_rect(fill = 'white'),
            plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
            axis.title = element_text(family = 'Calibri',face = 'bold'),
            axis.text = element_text(family = 'Calibri'),
            legend.text = element_text(family = 'Calibri',face = 'bold'),
            legend.position = c(0.92, 0.91),
            legend.key = element_rect(fill = NA,color = NA),
            legend.title = element_blank(),
            legend.background = element_blank()
      )
      
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      相关资源
      最近更新 更多