1

I am new to using qplot and ggplot, and basically want to make a figure that is just the combination of a bar plot and a line plot. I can do one or the other, but don't know how to do both at once!

Here is my data:

bulk = data.frame(x_pos=c(1,2,3,4,5,6,7,8), 
    y_line=c(3,7,6,8,14,16,18,12),
    y_bar=c(0,0,10,0,0,0,10,0))

For a line graph, I just do qplot(x_pos, y_line, data=bulk, geom="line") For a bar plot, I just do qplot(x_pos, y_bar, data=bulk)

But! How can I combine these at once into a single figure?? My real intention is to use several (maybe 6-10) different graphics techniques like this to generate complex figures, but it all starts with knowing how to do two at once. Thanks for any help!

1 Answer 1

5

Don't use qplot for this.

library(ggplot2)
ggplot(bulk, aes(x=x_pos)) + 
  geom_bar(aes(y=y_bar), stat="identity") +
  geom_line(aes(y=y_line), color="red", size=2)

enter image description here

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

2 Comments

Thanks so much for your answer, but can you please explain to me what aes does, as well as stat="identity"?
aes defines aesthetic mappings and stat="identity" overrides the default stat="bin" in geom_bar since your data is already binned. All of this is explained in the documentation.

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.