- Layers
ggplot()
vs.qplot()
ggplot()
vs. qplot()
We will be using the NBA draft data set.
nba <- read.csv("NBA Draft Class.csv")
This data has the same context - a common time and common place
ggplot2
we will superimpose data onto this grid in layersTo give you an idea…
library(ggplot2) p <- ggplot() # Empty canvas p
Now we add some points
p <- p + geom_point(data = nba, aes(x = Points.Per.Game, y = Win.Share, colour = Year), show.legend = T) p
Now we change the color scale of the points
p <- p + scale_colour_gradient(high = c("blue","green")) p
Now we add a title
p <- p + ggtitle("Win Shares vs Points Per Game") p
Now we add axes labels
p <- p + labs(x = "Points Per Game", y = " Win Shares") p
Now we edit some ascthetics
p <- p + theme(plot.title = element_text(hjust = .5, face = "bold", colour = "blue", size = 25)) p
There are a lot of parameters and many, many, many more things that can be added or done differently. https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf provides excellent information and is well documented.
qplot()
vs. ggplot()
qplot()
stands for "quickplot":
ggplot()
stands for "grammar of graphics plot"
qplot()
vs. ggplot()
Different ways to construct the same plot:
qplot(Points.Per.Game, Win.Share, colour = Year, data = nba, main = "Win Shares vs. Points Per Game")
or:
ggplot() + geom_point(data = nba, aes(x = Points.Per.Game, y = Win.Share, colour = Year), show.legend = T) + ggtitle("Win Shares vs. Points Per Game")
even this works:
ggplot(data = nba, aes(x = Points.Per.Game, y = Win.Share, colour = Year)) + geom_point()+ ggtitle("Win Shares vs. Points Per Game")
A layer added ggplot()
can be a geom…
… or a position adjustment to the scales
Plot | Geom | Stat |
---|---|---|
Scatterplot | point | identity |
Histogram | bar | bin count |
Smoother | line + ribbon | smoother function |
Binned Scatterplot | rectange + color | 2d bin count |
More geoms described at http://docs.ggplot2.org/current/
ggplot()
statement that creates this plot:Edit the plot to add a centered titled and labeled axes without the periods.
Change the shape of each point with respect to groups (Lookup documentation if needed).
# One of many that will produce the same plot ggplot( aes(x = Rebounds.Per.Game, y = Win.Share, colour = Position), data = nba) + geom_point()
ggplot( aes(x = Rebounds.Per.Game, y = Win.Share, colour = Position), data = nba) + geom_point()+ ggtitle("Win Shares vs. Rebounds Per Game") + labs(x = " Rebounds Per Game", y = "Win Shares") + theme(plot.title = element_text(hjust = .5))
ggplot( aes(x = Rebounds.Per.Game, y = Win.Share, colour = Position), data = nba) + geom_point(aes(shape = Position)) + ggtitle("Win Shares vs. Rebounds Per Game") + labs(x = " Rebounds Per Game", y = "Win Shares") + theme(plot.title = element_text(hjust = .5))