Skip to content

Instantly share code, notes, and snippets.

@alexstorer
Created October 8, 2013 18:06
Show Gist options
  • Save alexstorer/6888890 to your computer and use it in GitHub Desktop.
Save alexstorer/6888890 to your computer and use it in GitHub Desktop.

ggplot2 Qustions

I'm not sure why there are differences between these two seemingly similar ways of plotting using ggplot2 and its geom_point options.

library(ggplot2)
data(mtcars)

If our goal is to plot two separate variables on the same plot, the slides suggest that we do the following:

ggplot(mtcars, aes(x=wt))+
  geom_point(aes(y=disp),color="red") +
  geom_point(aes(y=hp),color="blue")

In general, we can make plots in ggplot2 by assigning the plots and "adding" things to them.

p <- ggplot(mtcars, aes(x=wt))
p + geom_point(aes(y=disp),color="red") + 
  geom_point(aes(y=hp),color="blue")

If we do the addition in multiple steps, however, the plot will be overwritten as opposed to added on to.

p <- ggplot(mtcars, aes(x=wt))
p + geom_point(aes(y=disp),color="red") + 
  geom_point(aes(y=hp),color="blue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment