ggplot2 从图例中删除元素

ggplot2 Delete elements from legend

我有这个情节:

根据此数据制作的:

   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)。事实上,show.legend = F是许多ggplot2层中的一个选项。

你可以试试这个:

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()
  )