从ggplot2中删除四个中的两个图例

时间:2018-05-27 00:21:16

标签: r ggplot2 label

我的数据框如下

validates :terms, acceptance: {
    message: ->(person) "%{person.name} must accept our terms!"
}

我可以创建以下条形图

   School Gender Value ColorGroup
1 School1   Male    10      1Male
2 School1 Female    30    1Female
3 School2   Male    40      1Male
4 School2 Female    70    1Female
5 School3   Male     5      2Male
6 School3 Female    90    2Female

enter image description here

我可以通过在代码中添加以下内容来隐藏所有图例。

cols33 <- c("1Male" = "yellow", "1Female" = "orange", "2Male" = "red", "2Female" = "blue") 

ggplot(data=data2, aes(x=School, y=Value, group = Gender,fill = ColorGroup)) + 
  geom_bar(stat = "identity", position = position_dodge(), width =  0.5) + 
  scale_fill_manual(name="",values=cols33)

enter image description here

我想做的只是保留School3的传说并删除School1和School2的传说。

我还在我的代码中使用了两个geom_bar命令并在其中一个中指定了show.legend = FALSE但是它不起作用我尝试了以下内容

guides(fill = FALSE)

enter image description here

请您帮我理解如何实现这一目标。

添加dput(data2)

ggplot(data=data2, aes(x=School, y=Value, group = Gender)) + 
  geom_bar(stat = "identity", position = position_dodge(), aes(fill = ColorGroup),width =  0.5, show.legend = FALSE) + 
  geom_bar(data = subset(data2,School == "School3"),aes(fill = ColorGroup),stat = "identity", position = position_dodge(), width =  0.5) + 
  scale_fill_manual(values=cols33) 

0 个答案:

没有答案