Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

r - ggplot each group consists of only one observation

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...