First load the ggplot2 package
library("ggplot2")
and the OK Cupid data set
data(okcupid, package = "jrTidyverse")
geom_point()
command# alpha makes the points transparent ggplot(data = okcupid) + geom_point(aes(x = age, y = height), alpha = 0.2)
g = ggplot(data = okcupid) g1 = g + geom_point(aes(x = age, y = height), alpha = 0.2)
So now running g1
will produce the graph
g1
x
and y
here are called aesthetics. What do you think happens if you omit the y
aesthetic?# this gives an error
For geom_point()
, both the x
and y
aesthetics are required. This particular geom has other aesthetics: shape, colour, size and alpha.^[These are available for most geoms. For a collection of aesthetics see the relevant help pages.] For instance we can specify that we wish to map the variable sex
to a colour aesthetic by including it inside aes()
r
g + geom_point(aes(x = age, y = height, colour = sex))
Change colour = sex
to colour = height
. Why do you think there's a change in the legend?
geom_bar()
can be used to create a bar chart. It requires only one aesthetic and that is x
. For the provided aesthetic, the frequencies will be calculated and shown as a bar. For example
g + geom_bar(aes(x = body_type))
+ xlab("Body Type")
and + ylab("Total")
g + geom_bar(aes(x = body_type)) + xlab("Body Type") + ylab("Total")
colour
and fill
). # What happens if you only have colour or only fill? g + geom_bar(aes(x = body_type, colour = sex, fill = sex))
x
and y
axes using a coord_flip()
layer added to your graph.g + geom_bar(aes(x = body_type, colour = sex, fill = sex)) + coord_flip()
I am not too keen on how the female and male bars are displayed on top of each other. The argument to change these is position
. The default is stack
, for example we can put the bars next to each other using
r
g + geom_bar(aes(x = body_type, colour = sex, fill = sex),
position = "dodge") +
coord_flip()
Other values you might try here are position = fill
, position = jitter
, or position = identity
.
What does the fill
position argument do?
g + geom_bar(aes(x = body_type, colour = sex, fill = sex), position = "fill") + coord_flip() # puts the values on a common scale (all sum to 1)
Box plots are a great way to visualise the shape of a distribution of some variable. Start by creating a boxplot of peoples ages conditional on drinking
g = ggplot(okcupid) g + geom_boxplot(aes(x = drinks, y = age))
x = drinks
for x = smokes
to get a boxplot for each groupg + geom_boxplot(aes(x = smokes, y = age))
facet_wrap(~ sex)
layer. Give it a try.g + geom_boxplot(aes(x = smokes, y = age)) + facet_wrap(~sex)
g + geom_bar(aes(x = body_type, colour = sex, fill = sex), position = "fill") + coord_flip() + facet_wrap(~ orientation)
This graph tells lets us compare the proportions of males and females for each body type, amongst the bisexual, gay and straight populations on okcupid. Experiment with different columns for facetting, e.g. diet
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.