R is a free software environment for statistical computing and graphics. — R-Projekt
R is a free software environment for statistical computing and graphics. — R-Projekt
There are three major graphical systems:
lattice
ggplot2
R is a free software environment for statistical computing and graphics. — R-Projekt
There are three major graphical systems:
lattice
ggplot2
In this course, we will focus on ggplot2
. If you want to learn about the others,
R Graphics
is a great source.
The data, mappings, statistical transformations and geometric objects form a layer. A plot can have multiple layers.
library(ggplot2)data("diamonds")ggplot(data = diamonds) + geom_point(mapping = aes(x = carat, y = price))
library(ggplot2)data("diamonds")ggplot(data = diamonds) + geom_point(mapping = aes(x = carat, y = price))
ggplot()
library(ggplot2)data("diamonds")ggplot(data = diamonds) + geom_point(mapping = aes(x = carat, y = price))
ggplot()
geom_point()
geom_*
function takes a mapping argument, which paired with aes()
defines how variables in your data are mapped to visual properties.not the whole dataset included
To make the (possibly nonlinear) relationship in the data easier visible we add a smoothing function to the plot.
To make the (possibly nonlinear) relationship in the data easier visible we add a smoothing function to the plot.
ggplot(data = diamonds) + geom_point(mapping = aes(x = carat, y = price)) + geom_smooth(mapping = aes(x = carat, y = price), method = 'loess')
To make the (possibly nonlinear) relationship in the data easier visible we add a smoothing function to the plot.
ggplot(data = diamonds) + geom_point(mapping = aes(x = carat, y = price)) + geom_smooth(mapping = aes(x = carat, y = price), method = 'loess')
To write more compact code we can
mapping
to ggplot()
. To make the (possibly nonlinear) relationship in the data easier visible we add a smoothing function to the plot.
ggplot(data = diamonds) + geom_point(mapping = aes(x = carat, y = price)) + geom_smooth(mapping = aes(x = carat, y = price), method = 'loess')
To write more compact code we can
mapping
to ggplot()
. The same mapping is then used for all layers (but can be overwritten if necessary).
To make the (possibly nonlinear) relationship in the data easier visible we add a smoothing function to the plot.
ggplot(data = diamonds) + geom_point(mapping = aes(x = carat, y = price)) + geom_smooth(mapping = aes(x = carat, y = price), method = 'loess')
To write more compact code we can
mapping
to ggplot()
. The same mapping is then used for all layers (but can be overwritten if necessary).
ggplot(diamonds, aes(carat, price)) + geom_point() + geom_smooth(method = 'loess')
?geom_point()
tells us about further aesthetics that we can map data to. ?geom_point()
tells us about further aesthetics that we can map data to. ggplot(diamonds, aes(x = carat, y = price, color = color, shape = cut)) + geom_point()
geom_point()
using the mtcars
data set. Try out different aesthetics with different variables. What do you note? Specifically explain the different behaviour of factor and numeric variables for e.g. color.
ggplot(diamonds, aes(x = carat, y = price, color = color, shape = cut)) + geom_point() + geom_smooth()
geom_point()
.ggplot(diamonds, aes(x = carat, y = price)) + geom_point(aes(color = color, shape = cut)) + geom_smooth()
ggplot()
(e.g. if there are many layers) and overwrite the created groups by an arbitrary constant value.ggplot(diamonds, aes(x = carat, y = price, color = color, shape = cut)) + geom_point() + geom_smooth(aes(group = 1))
geom_*
) or a statistical transformation (stat_*
).Each geometric object is associated with a default statistical transformation and vice versa.
Examples:
geom_point()
has the identity function as statistical transformation.
geom_smooth()
fits a regression model before plotting a line with a prediction interval.
stat_smooth()
is an alias which does essentially the same. However, in the first case we could change the statistic transformation and in the second case we could change the geometric object.
geom_point(stat = "smooth", method = "lm")
) but we will see an example where it can be useful.geom_bar()
counts the number of observations within each group and produces a bar plot.
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut)) # x should be discrete
geom_bar()
counts the number of observations within each group and produces a bar plot.
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut)) # x should be discrete
ggplot(diamonds, aes(x = depth, fill = cut)) + geom_histogram(binwidth = 0.1)
ggplot(diamonds, aes(x = depth, fill = cut)) + geom_histogram(binwidth = 0.1)
ggplot(diamonds, aes(x = depth, fill = cut)) + # try color instead of fill geom_density()
ggplot(diamonds, aes(x = depth, fill = cut)) + # try color instead of fill geom_density()
ggplot(diamonds, aes(carat, price)) + geom_point() + geom_density2d()
ggplot(diamonds, aes(carat, price)) + geom_point() + geom_density2d()
geom = "polygon"
ggplot(diamonds, aes(carat, price)) + geom_point() + stat_density2d(aes(fill = ..level..), geom = "polygon")
geom = "polygon"
ggplot(diamonds, aes(carat, price)) + geom_point() + stat_density2d(aes(fill = ..level..), geom = "polygon")
ggplot(diamonds, aes(carat, price)) + geom_point() + facet_grid( ~ cut)
ggplot(diamonds, aes(carat, price)) + geom_point() + facet_grid( ~ cut)
ggplot(data = diamonds, mapping = aes(carat, price)) + geom_point() + facet_grid(color ~ cut)
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "stack") # the default
Each geometric object has a parameter for position adjustment.
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "stack") # the default
Instead of stacking the bars we can position them side-by-side using dodge
.
geom_bar(., position = "dodge")
Each geometric object has a parameter for position adjustment.
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "stack") # the default
Instead of stacking the bars we can position them side-by-side using dodge
.
geom_bar(., position = "dodge")
With fill
relative proportions can be compared.
geom_bar(., position = "fill")
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "stack") # the default
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "stack") # the default
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
scale_aestheticname_scalename()
.scale_aestheticname_scalename()
.ggplot(diamonds, aes(carat, price, color = cut)) + geom_point() + scale_color_grey()
scale_aestheticname_scalename()
.ggplot(diamonds, aes(carat, price, color = cut)) + geom_point() + scale_color_grey()
ggplot(diamonds, aes(carat, price, color = cut)) + geom_point() + scale_color_manual(values = c("#c9792e", "blue", "green", "gray", "thistle2"))
ggplot(diamonds, aes(carat, price, color = cut)) + geom_point() + scale_color_manual(values = c("#c9792e", "blue", "green", "gray", "thistle2"))
labs()
allows you to
labs()
allows you to
ggplot(diamonds, aes(x = carat, y = price, fill = cut)) + geom_point() + labs(title = "Diamanten", x = "Karat", y = "Preis", fill = "Schnitt")
ggplot(diamonds, aes(x = carat, y = price, fill = cut)) + geom_point() + labs(title = "Diamanten", x = "Karat", y = "Preis", fill = "Schnitt")
ggplot(diamonds, aes(x = carat, y = price, fill = cut)) + geom_point() + labs(title = "Diamanten", x = "Karat", y = "Preis", fill = "Schnitt")
Themes control the appearance of the plot
Themes control the appearance of the plot
There are many predefined themes. However, if you like you may also define everything yourself.
Themes control the appearance of the plot
There are many predefined themes. However, if you like you may also define everything yourself.
ggplot(diamonds, aes(carat, price, color = cut)) + geom_point() + theme_bw()
ggplot(diamonds, aes(carat, price, color = cut)) + geom_point() + theme_bw()
ggplot(diamonds, aes(carat, price, color = cut)) + geom_point() + theme_bw()
facet_grid
)facet_grid
)library(cowplot)over1 <- ggplot(diamonds, aes(carat, price)) + geom_point()trans <- ggplot(diamonds, aes(carat, price)) + geom_point(alpha = 0.05)plot_grid(over1, trans)
over2 <- ggplot(mtcars, aes(am, cyl)) + geom_point() jitter <- ggplot(mtcars, aes(am, cyl)) + geom_jitter(width = 0.03, height = 0.1) plot_grid(over2, jitter) # from cowplot
ggplot2
such as cowplot
and ggExtra
. ggplot2
it is e.g. quite tricky to create a composite plot as
created by ggExtra::ggMarginal()
ggplot2
such as cowplot
and ggExtra
. ggplot2
it is e.g. quite tricky to create a composite plot as
created by ggExtra::ggMarginal()
library(ggExtra)scatter_plot <- ggplot(diamonds, aes(carat, price)) + geom_point()ggMarginal(scatter_plot, # ggExtra type = 'density', margins = 'both', size = 5, colour = '#FF0000', fill = '#FFA500' )
plot(mtcars$mpg ~ mtcars$wt, xlab = "wt", ylab = "mpg", pch = 19, ylim = c(5, 35))mod <- lm(mpg ~ wt, data = mtcars)abline(mod, col = "red")wt_new <- seq(min(mtcars$wt), max(mtcars$wt), by = 0.05)conf_interval <- predict(mod, newdata = data.frame(wt = wt_new), interval = "confidence", level = 0.95)# setup vertrices of polygon (for shading the CI):p <- cbind(c(wt_new, rev(wt_new)), c(conf_interval[, 3], rev(conf_interval[, 2])))polygon(p, col = adjustcolor("steelblue", alpha.f = 0.5), )lines(wt_new, conf_interval[, 2], col = "steelblue", lty = 2)lines(wt_new, conf_interval[, 3], col = "steelblue", lty = 2)
R is a free software environment for statistical computing and graphics. — R-Projekt
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |