ggplot:使用geom_line将y变量绘制为连续线

时间:2017-05-23 17:23:26

标签: r ggplot2

我试图在ggplot中使用geom_line绘制一些数据。但是,我想将y变量设置为连续而不是x变量。 (我的数据是关于湖中不同深度的养分浓度,所以如果连续深度变量在y轴上则更有意义。)

这是一些虚假数据。

library(ggplot2)
library(dplyr)
library(tidyr)
depth <- c(-2, -4, -6, -8, -10, -12, -14)
nutrient1 <- c(1, 1, 3, 1, 5, 1, 3)
nutrient2 <- c(2, 2, 4, 2, 8, 9, 3)
data <- as.data.frame(cbind(depth, nutrient1, nutrient2))
data.gathered <- data %>%
gather(key = nutrient, value = value, 2:3)

这是我试图策划的。 y的负值仅表示深度低于某个值。

ggplot(data.gathered, 
       aes(x = value, y = depth, colour = nutrient)) + 
    geom_line()

如果我这样绘制,我得到由x值(营养值)定义的连续线。我希望该行按照y值定义的顺序连接我的点,而不是x值。因此,营养素1的线应该进展为1,-2 - > 1,-4 - &gt; 3,-6 - &gt; 1,-8等。看起来应该很容易,但我一直在努力。

有谁知道如何解决这个问题?

谢谢!

0 个答案:

没有答案
相关问题