如何在一个中绘制几个线图

时间:2014-12-27 13:45:24

标签: r ggplot2 linegraph

我想用R(ggplot2)绘制我的身材。我想要一个像图像2的折线图。

这里是my.data:

B50K,B50K+1000C50K,B50K+2000C50K,B50K+4000C50K,B50K+8000C50K,gen,xaxile
0.3795,0.4192,0.4675,0.5357,0.6217,T18-Yield,B50K
0.3178,0.3758,0.4249,0.5010,0.5870,T20-Yield,B50K+1000C50K
0.2795,0.3266,0.3763,0.4636,0.5583,T21-Yield,B50K+2000C50K
0.2417,0.2599,0.2898,0.3291,0.3736,T18-Fertility,B50K+4000C50K
0.2002,0.2287,0.2531,0.2962,0.3485,T19-Fertility,B50K+8000C50K
0.1642,0.1911,0.2151,0.2544,0.2951,T20-Fertility

*** - >分隔符是“,”。顺便说一下,我没有任何有用或有用的.r脚本。

图示的图像显示了我在Microsoft Word中的数字。

enter image description here

我已经通过互联网尝试了几个脚本,但没有一个没有用过。

请你帮我一个.r脚本来读取我的数据文件,如img1,并绘制我的数据,如图所示。

1 个答案:

答案 0 :(得分:1)

诀窍是重塑您的数据(使用melt包中的reshape2),以便您可以轻松地将颜色和线型映射到gen

# Your data - note i also added an extra comma after the fifth column in row 6.
# It would be easier if you gave data using dput as described in comments above - thanks
dat <- read.table(text="B50K,B50K+1000C50K,B50K+2000C50K,B50K+4000C50K,B50K+8000C50K,xaxile,gen
0.3795,0.4192,0.4675,0.5357,0.6217,B50K,T18-Yield
0.3178,0.3758,0.4249,0.5010,0.5870,B50K+1000C50K,T20-Yield
0.2795,0.3266,0.3763,0.4636,0.5583,B50K+2000C50K,T21-Yield
0.2417,0.2599,0.2898,0.3291,0.3736,B50K+4000C50K,T18-Fertility
0.2002,0.2287,0.2531,0.2962,0.3485,B50K+8000C50K,T19-Fertility
0.1642,0.1911,0.2151,0.2544,0.2951,,T20-Fertility", 
header=T, sep=",", na.strings="")

# load the pckages you need
library(ggplot2)
library(reshape2)

# assume xaxile column is unneeded? - did you add this column yourself? 
dat$xaxile <- NULL

# reshape data for plotting
dat.m <- melt(dat)

# plot
ggplot(dat.m, aes(x=variable, y=value, colour=gen, 
                  shape=gen, linetype=gen, group=gen)) +
                  geom_point() + 
                  geom_line() 

然后,您可以使用scale_linetype_manualscale_shape_manual手动指定绘图的外观。 This post会有所帮助,但还有很多其他的