0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Fantastic, thanks! But one further question: why didn't I have to do that with the Orange example from the tutorial?
@PeterLewis Because in that case the color variable is already a factor, so ggplot can "safely" assume you meant it as a grouping variable. If it's a continuous variable, ggplot won't make any assumptions, since a continuous variable wouldn't generally be used for grouping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.