ggplot每个组只包含一个观察值 [英] ggplot each group consists of only one observation

查看:183
本文介绍了ggplot每个组只包含一个观察值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作类似于此答案的情节: https://stackoverflow.com/a/4877936/651779

我的数据框如下所示:

  df2 <  -  read.table(text ='measurement values value 
1 4hours sham1 6
2 1day sham1 175
3 3days sham1 417
4 7days sham1 163
5 14天sham1 37
6 90天sham1 134
7 4小时sham2 8
8 1day sham2 402
9 3days sham2 482
10 7天sham2 67
11 14天sham2 16
12 90days sham2 31
13 4hours sham3 185
14 1day sham3 402
15 3days sham3 482
16 7days sham3 85
17 14days sham3 29
18 90天sham3 10',header = T)

然后用

  ggplot(df2,aes(measurements,value))+ geom_line(aes(color = samples))

剧情中没有行显示,我收到消息

  geom_path:每个组包含只有一个观察。 
您是否需要调整团体美学?

我不知道我在做什么与上面链接的答案不同。我应该改变什么来完成这项工作?

添加 group = samples 添加到 geom_line aes 中。这是必要的,因为您需要每行 samples 而不是每个数据点的一行。

  ggplot(df2,aes(测量值))+ 
geom_line(aes(color = samples,group = samples))


I'm trying to make a plot similar to this answer: https://stackoverflow.com/a/4877936/651779

My data frame looks like this:

df2 <- read.table(text='measurements samples value
1        4hours   sham1     6
2          1day   sham1   175
3         3days   sham1   417
4         7days   sham1   163
5        14days   sham1    37
6        90days   sham1   134
7        4hours   sham2     8
8          1day   sham2   402
9         3days   sham2   482
10        7days   sham2    67
11       14days   sham2    16
12       90days   sham2    31
13       4hours   sham3   185
14         1day   sham3   402
15        3days   sham3   482
16        7days   sham3    85
17       14days   sham3    29
18       90days   sham3    10',header=T)

And plot it with

ggplot(df2, aes(measurements, value)) + geom_line(aes(colour = samples))

No lines show in the plot, and I get the message

geom_path: Each group consist of only one observation. 
Do you need to adjust the group aesthetic?

I don't see where what I'm doing is different from the answer I linked above. What should I change to make this work?

解决方案

Add group = samples to the aes of geom_line. This is necessary since you want one line per samples rather than for each data point.

ggplot(df2, aes(measurements, value)) + 
  geom_line(aes(colour = samples, group = samples))

这篇关于ggplot每个组只包含一个观察值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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