有没有办法从 ggplot 制作的图例中删除项目?
我使用 ggplot 在 R 中创建了一个图表,它显示了多年来不同的浮游植物群。现在,有人要求从图例中删除一个组,因为它们在图中无法真正看到(例如“蓝细菌”)。我的问题这可能吗?如果是的话怎么办?
我的数据:
我的代码:
if(!require(pacman)){install.packages("pacman")}
pacman::p_load(DT,leaflet,leaflet.extras,readxl,htmlwidgets,plotly,dplyr,lubridate,vegan,zoo,
ggplot2,reshape2,sciplot,psych,RColorBrewer)
df<-read_excel("C:/wherever")
attach(df)
colourCount=length(unique(df$Phytoplankton))
getPalette = colorRampPalette(brewer.pal(12, "Paired"))
p<-ggplot(data=df, aes(x=Year, y=Abu, fill=Phytoplankton))+geom_bar(stat="identity",color="black", width =1)
p<-p+scale_fill_manual(values = getPalette(colourCount))
p<-p+labs(title="Phytoplanktondichte",x="Jahr", y = "Individuendichte (Ind/L)")
p<-p+theme_minimal()
p
我找到了如何更改颜色、位置、标题基本上所有内容,但是我不知道如何从图例中删除某些内容。
我非常感谢你的帮助。
I have created a graph in R with ggplot
, which shows the different groups of phytoplankton over the years. Now it have been requested to remove a group from the legend, because they cannot really been seen in the graph (for eg. "Cyanobacteria"). My question is this possible? If yes how?
My data:
My code:
if(!require(pacman)){install.packages("pacman")}
pacman::p_load(DT,leaflet,leaflet.extras,readxl,htmlwidgets,plotly,dplyr,lubridate,vegan,zoo,
ggplot2,reshape2,sciplot,psych,RColorBrewer)
df<-read_excel("C:/wherever")
attach(df)
colourCount=length(unique(df$Phytoplankton))
getPalette = colorRampPalette(brewer.pal(12, "Paired"))
p<-ggplot(data=df, aes(x=Year, y=Abu, fill=Phytoplankton))+geom_bar(stat="identity",color="black", width =1)
p<-p+scale_fill_manual(values = getPalette(colourCount))
p<-p+labs(title="Phytoplanktondichte",x="Jahr", y = "Individuendichte (Ind/L)")
p<-p+theme_minimal()
p
I found out how to change the colour, the position, the title basically everything, but I coundn´t fine anything in how to remove something from the legend.
I would really appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望图表中包含未识别的元素(它们被绘制但未在图例中标记),您可以在
breaks
参数中指定图例中所需的值,同时仍将所有比例值指定为命名的矢量:If you want unidentified elements in your graph (they are plotted but not labeled in the legend), you can specify what values you want in the legend in the
breaks
argument while still specifying all scale values as a named vector: