Peter Lewis
Peter Lewis

Reputation: 97

Why doesn't qplot plot lines in multiple series for this data file?

It's my first day learning R and ggplot. I've followed some tutorials and would like plots like are generated by the following command:

qplot(age, circumference, data = Orange, geom = c("point", "line"), colour = Tree)

It looks like the figure on this page: http://www.r-bloggers.com/quick-introduction-to-ggplot2/

I had a handmade test data file I created, which looks like this:

        site    temp    humidity
1       1       1       3
2       1       2       4.5
3       1       12      8
4       1       14      10
5       2       1       5
6       2       3       9
7       2       4       6
8       2       8       7

but when I try to read and plot it with:

test <- read.table('test.data')
qplot(temp, humidity, data = test, color=site, geom = c("point", "line"))

the lines on the plot aren't separate series, but link together:

https://i.sstatic.net/emnpb.jpg

What am I doing wrong?

Thanks.

Upvotes: 0

Views: 1027

Answers (1)

joran
joran

Reputation: 173567

You need to tell ggplot2 how to group the data into separate lines. It's not a mind reader! ;)

dat <- read.table(text = "        site    temp    humidity
1       1       1       3
2       1       2       4.5
3       1       12      8
4       1       14      10
5       2       1       5
6       2       3       9
7       2       4       6
8       2       8       7",sep = "",header = TRUE)

qplot(temp, humidity, data = dat, group = site,color=site, geom = c("point", "line"))

enter image description here

Note that you probably also wanted to do color = factor(site) in order to force a discrete color scale, rather than a continuous one.

Upvotes: 2

Related Questions