我的geom_line()在ggplot2中发生了什么?

时间:2015-02-24 18:34:07

标签: r ggplot2 aesthetics

我不是R的专家,但我多次使用过ggplot2而且没有任何问题。不过,这次我无法在我的图表中绘制线条而且我不知道为什么(它应该是非常简单的东西)。

例如:

   def.percent    period
1    5.0657339 1984-1985
2    3.9164528 1985-1986
3    -1.756613 1986-1987
4    2.8184863 1987-1988
5    -2.606311 1988-1989

我必须编码:

ggplot(plot.tab, aes(x=period, y=def.percent)) + geom_line() + geom_point() + ggtitle("Deforestation rates within Properties")

当我运行它时,它只是绘制没有线的点。它还给了我这样的信息:

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

这不是一个错误,但我无法弄明白如何绘制线条...任何想法?

1 个答案:

答案 0 :(得分:1)

您的x轴(period)是一个因子而不是数字,因此它不会连接它们。你可以通过在美学中设置group = 1来解决这个问题,它告诉ggplot2将它们全部组合成一行:

ggplot(plot.tab, aes(x = period, y = def.percent, group = 1)) +
    geom_line() +
    geom_point() +
    ggtitle("Deforestation rates within Properties")