使用ggplot2绘制数据框中的多行 [英] Plotting multiple lines from a data frame with ggplot2

查看:99
本文介绍了使用ggplot2绘制数据框中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2绘制多行。我的数据被安装到数据框中,如下所示:

 > rs 
时间1 2 3 4
1 200 17230622635 17280401147 17296993985 17313586822
2 400 22328386154 22456712709 22499488227 22542263745
3 600 28958840968 29186097622 29261849840 29337602058
4 800 40251281810 40650094691 40783032318 40915969945
5 1000 73705771414 74612829244 74915181854 75217534464

我想使用time列作为x值。其他列是不同行中点的y值。在上面的数据中,有4行,每行包含5个点。更具体地说,第一行具有点(200,17230622635),(400,22328386154),(600,28958840968)等。第二行具有点(200,17280401147),(400,22456712709)等(如果你需要数据格式的进一步解释,最后见PS。)

要生成一个类似的数据,你可以使用下面的代码:

  rs = data.frame(seq(200,1000,by = 200),runif(5),runif(5),runif(5))
names(rs)= c(time,1:3)



我首先将数据融入长格式:

  library('reshape2')
library('ggplot2')
melted = melt(rs,id.vars =time )

然后使用以下数据绘制数据:

  ggplot()+ geom_line(data = fusion,aes(x =time,y =value,group =variable))

Ho

任何人都可以帮我看看我的程序有什么问题吗?



PS

关于数据格式您可以想象有很多学生该班,我们有他们的几个测验分数。每行包含一个学生的数据:第一列是测验编号,其余列是他/她的分数。对于每个学生,我们想划一条线来反映他/她的分数在不同测验中的变化情况,每个分数都是某个学生的一次测验的分数。由于有多名学生,我们希望绘制多行。



关于融化数据



具体到上面显示的数据,我从 melt()函数获得的数据是:

 >融化的
时间变量值
1 200 1 17230622635
2 400 1 22328386154
3 600 1 28958840968
4 800 1 40251281810
5 1000 1 73705771414
6 200 2 17280401147
7 400 2 22456712709
8 600 2 29186097622
9 800 2 40650094691
10 1000 2 74612829244
11 200 3 17296993985
12 400 3 22499488227
13 600 3 29261849840
14 800 3 40783032318
15 1000 3 74915181854
16 200 4 17313586822
17 400 4 22542263745
18 600 4 29337602058
19 800 4 40915969945
20 1000 4 75217534464


解决方案

x = time,y = value,group = variable))+ geom_line()

ggplot美学

I am trying to plot multiple lines using ggplot2. My data is fitted into a data frame as follow:

> rs
  time           1           2           3           4
1  200 17230622635 17280401147 17296993985 17313586822
2  400 22328386154 22456712709 22499488227 22542263745
3  600 28958840968 29186097622 29261849840 29337602058
4  800 40251281810 40650094691 40783032318 40915969945
5 1000 73705771414 74612829244 74915181854 75217534464

I would like to use the "time" column as the x value. Other columns are y values of points in different lines. In the data above, there are 4 lines, each line consists of 5 points. More specifically, the first line has points (200, 17230622635), (400, 22328386154), (600, 28958840968), etc. The second line has points (200, 17280401147), (400, 22456712709), etc. (If you need further explanation of the data format, see P.S. in the end.)

To generate a similar data, you could use the following code:

rs = data.frame(seq(200, 1000, by=200), runif(5), runif(5), runif(5))
names(rs)=c("time", 1:3)

I followed some examples on stack overflow and tried to use reshape2 and ggplot2 to do this plot:

I first melt the data into a "long-format":

library('reshape2')
library('ggplot2')
melted = melt(rs, id.vars="time")

Then plot the data using the following statment:

ggplot() + geom_line(data=melted, aes(x="time", y="value", group="variable"))

However, I got an empty graph which has no point nor line.

Can anyone help me to see what's wrong with my procedure?

P.S.

About the data format:

You can imagine there are many students in the class and we have their scores of several quizzes. Each row contains one student's data: first column is the quiz number, then the rest of columns are his/her scores. For each student, we want to plot a line to reflect how his/her scores change over different quizzes, each point is the score of one quiz for a certain students. Since there are multiple students, we would like to draw multiple lines.

About the melted data:

Specific to the data I show above, the data I got from the melt() function is:

> melted
   time variable       value
1   200        1 17230622635
2   400        1 22328386154
3   600        1 28958840968
4   800        1 40251281810
5  1000        1 73705771414
6   200        2 17280401147
7   400        2 22456712709
8   600        2 29186097622
9   800        2 40650094691
10 1000        2 74612829244
11  200        3 17296993985
12  400        3 22499488227
13  600        3 29261849840
14  800        3 40783032318
15 1000        3 74915181854
16  200        4 17313586822
17  400        4 22542263745
18  600        4 29337602058
19  800        4 40915969945
20 1000        4 75217534464

解决方案

Drop the quotes:

ggplot(data=melted, aes(x=time, y=value, group=variable)) + geom_line()

see: ggplot aesthetics

这篇关于使用ggplot2绘制数据框中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆