15 April 2019
ggplot2
ggplot2
R
has numerous plotting functions in the base package graphics
?plot ?boxplot ?hist
Go to the Examples
at the bottom of each help page and copy a few lines
ggplot2
ggplot2
gives much more flexibility and power
tidyverse
ggplot2
: aestheticsThe main function is ggplot()
aes()
ggplot(transport, aes(x = weight, y = height))
No data will be plotted. We get the plot area only…
ggplot2
: geometry+
symbol at the end of the linegeom_...()
functionsggplot(transport, aes(x = weight, y = height)) + geom_point()
ggplot2
: geometryggplot2
: aestheticsThere are numerous aesthetics available for geom_point()
?geom_point
ggplot(transport, aes(x = weight, y = height, colour = method)) + geom_point()
ggplot(transport, aes(x = weight, y = height, colour = method, shape = gender)) + geom_point()
ggplot2
: aestheticsWe can put the general aesthetics in ggplot()
, with the geom_point()
specific ones in that line
ggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender))
ggplot()
are passed to all geomsggplot2
: aestheticsAesthetics set outside of aes()
are general across all points
ggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender), size = 4)
ggplot2
: adding multiple geomsggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender)) + geom_smooth()
This defaults to a loess
fit
ggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender)) + geom_smooth(method = "lm", formula = y~x, se = FALSE)
ggplot2
: labelsPoint labels can be added using geom_text()
ggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender)) + geom_smooth(method = "lm", formula = y~x, se = FALSE) + geom_text(aes(label= name)) + labs(x = "Weight (kg)", y = "Height (cm)", shape = "Gender", colour = "Transport")
ggplot2
: labelsThey tend to be clumsy so
library(ggrepel) ggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender)) + geom_smooth(method = "lm", formula = y~x, se = FALSE) + geom_text_repel(aes(label= name)) + labs(x = "Weight (kg)", y = "Height (cm)", shape = "Gender", colour = "Transport")
ggplot2
: labelsAxis and legend labels can be added using labs()
ggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender)) + geom_smooth(method = "lm", formula = y~x, se = FALSE) + geom_text_repel(aes(label= name)) + labs(x = "Weight (kg)", y = "Height (cm)", shape = "Gender", colour = "Transport")
ggplot2
: facets(This is my favourite feature)
ggplot(transport, aes(x = weight, y = height)) + geom_point(aes(colour = method, shape = gender)) + geom_smooth(method = "lm", formula = y~x, se = FALSE) + geom_text_repel(aes(label= name)) + labs(x = "Weight (kg)", y = "Height (cm)", shape = "Gender", colour = "Transport") + facet_wrap(~gender)
ggplot2
: Different geomsEnter geom_
in the Console followed by the tab
key
ggplot(transport, aes(x = height, fill = gender)) + geom_density(alpha = 0.5)
ggplot(transport, aes(x = gender, y =height, fill = gender)) + geom_boxplot()
ggplot2
: geom_bar()
We can summarise our data before plotting
transport %>% filter(!is.na(height)) %>% group_by(method, gender) %>% summarise(mn_height = mean(height), sd_height = sd(height)) %>% ggplot(aes(x = method, y = mn_height, fill = method)) + geom_bar(stat = "identity") + facet_wrap(~gender) + guides(fill = FALSE)
NB: geom_bar()
requires stat = "identity"
ggplot2
: geom_errorbar()
transport %>% filter(!is.na(height)) %>% group_by(method, gender) %>% summarise(mn_height = mean(height), sd_height = sd(height)) %>% ggplot(aes(x = method, y = mn_height, fill = method)) + geom_bar(stat = "identity") + geom_errorbar(aes(ymin = mn_height - sd_height, ymax = mn_height + sd_height), width = 0.6)+ facet_wrap(~gender) + guides(fill =FALSE)
These are not intuitive so here's how:
transport %>% filter(!is.na(height)) %>% group_by(method) %>% summarise(n = n()) %>% ggplot(aes(x = 1, y = n, fill = method)) + geom_bar(stat = "identity", colour = "black") + coord_polar("y") + theme_void()
ggplot2
: facetsHow could we get histograms for both weight
and height
using facets?
geom_histogram()
ggplot2
: facetsHow could we get histograms for both weight
and height
using facets?
transport %>% gather(key = "measurement", value = "value", ends_with("ght")) %>% ggplot(aes(x = value, fill = measurement)) + geom_histogram(bins = 10, colour = "black") + facet_wrap(~measurement, scales = "free_x") + guides(fill = FALSE)
ggplot2
: facetstransport %>% gather(key = "measurement", value = "value", ends_with("ght")) %>% ggplot(aes(x = gender, y = value, fill = gender)) + geom_boxplot() + facet_wrap(~measurement, scales = "free_y")