有没有办法从 ggplot 制作的图例中删除项目?

发布于 2025-01-10 11:46:31 字数 941 浏览 1 评论 0原文

我使用 ggplot 在 R 中创建了一个图表,它显示了多年来不同的浮游植物群。现在,有人要求从图例中删除一个组,因为它们在图中无法真正看到(例如“蓝细菌”)。我的问题这可能吗?如果是的话怎么办?

我的数据:

data phytoplankton

我的代码:

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:

data phytoplankton

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2025-01-17 11:46:31

如果您希望图表中包含未识别的元素(它们被绘制但未在图例中标记),您可以在 breaks 参数中指定图例中所需的值,同时仍将所有比例值指定为命名的矢量:

ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
  geom_point() +
  scale_color_manual(
    breaks = c("4", "6"),
    values = c("4" = "orange", "6" = "blue", "8" = "purple")
  )

enter图片描述这里

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:

ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
  geom_point() +
  scale_color_manual(
    breaks = c("4", "6"),
    values = c("4" = "orange", "6" = "blue", "8" = "purple")
  )

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文