2016-01-01 57 views
-1

这不应该是这么难,但我卡住了。我有以下形式的表:line plot with R

 | 1  | 2 | 3 
    ------------------------------------ 
    TypeA | 3213 | 2121 | 43  
    TypeB | 31321 | 321 | 10 
    TypeC | 332 | 11 | 9 

我想生成一个线图,三行:每种类型,其中x坐标是“1,2,3”,和y坐标是数字(3213,...)。我正在按照here中的步骤操作,但不知道如何迭代第一列。

+1

考虑对你的例子可重复的,并显示你试过什么。 –

回答

5

如果您在x轴上添加另一列来定义它们的值,则可以使用tidyr::gather收集数据并使用geom_line对其进行绘图。 theme_bw()是否有删除灰色背景。

xy <- data.frame(type = c("a", "b", "c"), one = runif(3), 
       two = runif(3), three = runif(3), seq = 1:3) 

library(tidyr) 

xyg <- gather(data = xy, typ, val, -seq, -type) 

library(ggplot2) 

ggplot(xyg, aes(x = seq, y = val, color = typ)) + 
    theme_bw() + 
    geom_line() 

enter image description here