2016-09-28 86 views
-3

我正在一个图上应该包含同一个图上的3个不同的行。我工作的数据帧是后续:
enter image description here绘制一个多行ggplot图

我希望能够用IND(我的数据点)在x轴上,然后绘制利用配有柱传来的数据3条不同的线, b和c。 我只设法画一条线。

你能帮我吗?我现在使用的代码是

ggplot(data=f, aes(x=ind, y=med, group=1)) + 
    geom_line(aes())+ geom_line(colour = "darkGrey", size = 3) + 
    theme_bw() + 
    theme(plot.background = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank()) 
+1

您需要将数据融入长格式。 –

+0

[如何做一个伟大的R可重现的例子?](http://stackoverflow.com/questions/5963269) – zx8754

回答

0

关键是将有问题的列传播到新变量中。这发生在以下代码中的gather()步骤中。其余的几乎是锅炉板ggplot2。

library(ggplot2) 
library(tidyr) 

xy <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10), 
       ind = 1:10) 

# we "spread" a and b into a a new variable 
xy <- gather(xy, key = myvariable, value = myvalue, a, b) 

ggplot(xy, aes(x = ind, y = myvalue, color = myvariable)) + 
    theme_bw() + 
    geom_line() 

enter image description here

0

熔体和ggplot:

df$ind <- 1:nrow(df) 
head(df) 
      a   b  med   c ind 
1 -87.21893 -84.72439 -75.78069 -70.87261 1 
2 -107.29747 -70.38214 -84.96422 -73.87297 2 
3 -106.13149 -105.12869 -75.09039 -62.61283 3 
4 -93.66255 -97.55444 -85.01982 -56.49110 4 
5 -88.73919 -95.80307 -77.11830 -47.72991 5 
6 -86.27068 -83.24604 -86.86626 -91.32508 6 

df <- melt(df, id='ind') 
ggplot(df, aes(ind, value, group=variable, col=variable)) + geom_line(lwd=2) 

enter image description here