ggplot图例(geom_point)中是否有空白条目?

时间:2019-04-17 20:26:11

标签: r ggplot2

我的目标是在ggplot中创建一个简单的散点图,但是我以前从未遇到过:图例条目带有彩色圆点,但没有标签,并且在图中本身不存在。有什么作用?

数据和可视化 这是要复制的数据:

year,rate,study
1987,0.014,"A"
1987,0.0184,"B"
1987,0.0283,"B"
1987,0.0186,"B"
1988,,
1989,,
1990,,
1991,0.0368,"B"
1991,0.0317,"B"
1991,0.0271,"B"
1992,,
1993,,
1994,,
1995,,
1996,0.039,"A"
1996,0.0616,"B"
1996,0.0626,"B"
1996,0.0591,"B"
1997,0.055,"C"
1998,,
1999,,
2000,,
2001,,
2002,0.0674,"D"
2003,,
2004,0.07,"C"
2004,0.0855,"E"
2005,,
2006,,
2007,,
2008,,
2009,,
2010,,
2011,0.089,"C"
2012,,
2013,,
2014,0.09,"E"
2015,,
2016,,

以下是绘图的代码(从csv作为d导入的数据集):

d_plot <- ggplot(data = d, aes(year, rate)) +
  geom_point(mapping = aes(x=year, y=rate, color = study))

最后,这是奇怪的图形(请注意A上方的点):

Note the dot above A

假设和解决方案

我猜测这与以下事实有关:很多年没有“费率”或“研究”,所以我经历了类似的文章,并尝试使用na.omit和{{ 1}}到处都是,但到目前为止没有任何效果。

任何人?提前致谢。

1 个答案:

答案 0 :(得分:0)

您说得对,因为年份没有rate。如果删除rateNA的行,则不会得到多余的点。

ggplot(data = d[!is.na(d$rate), ], aes(year, rate)) +
    geom_point(mapping = aes(x=year, y=rate, color = study))

picture without a dot