In this project, I will be using the in-built data set msleep in R to create a lolipop graph. The data show the amount of time spent asleep vs awake in many different orders of mammal. My lolipop plot will show the averages sleep of each order of mammal compared to the total average sleep time for all orders of mammal in the dataset.
I made this plot following a YouTube tutorial by the channel R Programming 101. It is a great resource to check out for learning R. Link to the video: https://www.youtube.com/watch?v=XbeYUzsdlgw
Loading the data set into my work space. Then I created a variable called mean_sleep that takes the mean of the sleep_total column for each of the orders of mammals. I then used the fct_reorder() function to make sure that the data were plotted in increasing order from left to right in the ggplot.
# Loading the data to my workspace
data("msleep")
# Manipulating the data
msleep %>%
group_by(order) %>%
summarise(mean_sleep = mean(sleep_total)) %>%
mutate(order = fct_reorder(order, mean_sleep)) %>%
# Creating the plot
ggplot(aes(x = order, y = mean_sleep)) +
labs(title = "Average Sleep for Various Orders of Mammals",
y = "Average Sleep Hours",
x = "") +
theme(axis.text.x = # Great to know for customizing text on plots
element_text(angle = 45,
vjust = 1,
hjust = 1),
axis.text.y = element_text(face = "bold"),
plot.title = element_text(hjust = 0.4,
vjust = 1,
size = 15,
face = "bold"
)) +
theme( # Great to know for customizing plot colors
axis.text.x = element_text(color = 'orange'),
axis.text.y = element_text(color = 'orange'),
axis.title.y = element_text(color = 'orange'),
plot.title = element_text(color = 'orange'),
axis.line = element_line(color = 'orange'),
axis.ticks = element_line(color = 'orange'),
panel.background = element_rect(fill = 'black'),
plot.background = element_rect(fill = 'black'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none"
) +
# Adding a horizontal line to represent the average sleep time for all orders of mammals
geom_hline(yintercept = (mean(msleep$sleep_total)),
color = 'darkred',
linewidth = 1) +
# Adding lines from each group's average to the total average horizontal line we drew on the graph.
# When using geom_segment() you need to define a start and end point.
geom_segment(aes(x = order,
y = mean(msleep$sleep_total), # defines start point
xend = order,
yend = mean_sleep), # defines end point
color = 'orange') +
# Lastly we add points for each groups mean sleep time + define a color gradient for the points.
geom_point(aes(color = mean_sleep),
size = 6) +
scale_color_gradient(low = 'hotpink', # scale_color_gradient() needs a low color and high color
high = 'yellow') +
# Adding an annotation for the plot
annotate("text",
x = 4,
y = 11.5,
label = "Average Sleep for All Mammals",
color = 'white',
fontface = "italic",
hjust = 0.4) +
# Adding an arrow
geom_curve(aes(x = 7.75,
y = 11,
xend = 8.35,
yend = 10.5),
color = 'white',
curvature = -0.35,
linewidth = 0.5,
arrow = arrow(length =
unit(0.01, "npc"),
type = "closed"))
## Warning in geom_curve(aes(x = 7.75, y = 11, xend = 8.35, yend = 10.5), color = "white", : All aesthetics have length 1, but the data has 19 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
Here is the finished graph
This project can be referenced when creating custom plots for helpful functions and code in ggplot2.