ggplot2无法绘制曲线

时间:2014-08-14 20:49:43

标签: r plot ggplot2 line

我试图在ggplot2中绘制一条曲线,如下所示:

enter image description here 但是,在ggplot2中,我只能通过以下方式画线:

enter image description here

以下是我用来创建两张图片的代码:

 df1 <- data.frame(dollar = c(0,5,10,20,30), value = c(0,200,300, -100, -300))
 # draw line graph with base plot
 plot(y = df1$dollar, x = df1$emiss_red, type = "l")
 # draw line graph with ggplot
 ggplot() + geom_line(data = df1, aes(y = dollar, x = value), size =1)

Ggplot2似乎根据x值对数据帧进行排序,然后根据x值连接点。但是,我不希望我的图表被订购。

此外,我不想翻转轴,因为美元值必须出现在y轴上。由于我更喜欢​​在ggplot2中创建这些图形,有人知道如何实现这一点吗?

1 个答案:

答案 0 :(得分:2)

您只需将geom_line交换为geom_path即可。如the documentation中所述,geom_path以原始顺序连接&#34;观察&#34;,而geom_line连接&#34;观察,按x值排序&#34;。

所以最后一行是

ggplot() + geom_path(data = df1, aes(y = dollar, x = value), size =1)