首页 > 解决方案 > 从ggplot中删除某些数据

问题描述

我正在按年份绘制物种丰度(计数)图,每个物种都有不同的颜色。我试图摆脱一些没有数据或数据微不足道的物种。到目前为止,我有这个:

 ggplot(data = df, mapping = aes(x = Year, y = Count,
                                 color = Species)) + geom_line() 

该图正是我想要的,但我不知道如何从图中删除一个物种的数据,所以它看起来更干净一些。

这是数据的样子:

  Year   Species      Count
1 1992 American Toad     2
2 1993 American Toad    12
3 1994 American Toad    13
4 1995 American Toad   120
5 1996 American Toad   144
6 1997 American Toad    82

标签: rggplot2

解决方案


正如评论中所建议的,您可以使用它dplyrfilter输出行:

library(dplyr)
library(ggplot2)

df %>%
    filter(Species != "unwanted species") %>%
    ggplot(mapping = aes(x = Year, y = Count, color = Species)) +
    geom_line() 

推荐阅读