
Geom_errorbar(aes(colour = sex, ymin = winglen - se, ymax = winglen + se), Geom_point(aes(colour = sex), position = position_dodge(width = 1)) + ) for each region: ggplot(data = buttersum, aes(x = spp, y = winglen)) + To plot North and South on separate facets, we tell facet_grid() to plot everything else (. We map one of the explanatory variables to the x-axis, one to different colours and one to the facets. Three are explanatory: species, sex and region. Our plot has the means and standard errors for each group and this requires us to summarize over the replicates which we can do with the summarySE() function: buttersum <- summarySE(data = butter, measurevar = "winglen", region where it is from, one of “North” or “South”.spp its species, one of “F.concocti” or “F.flappa”.

winglen the wing length (in millimeters) of an individual.

This each row in this data set is an individual butterfly and the columns are four variables: It can be read in to a dataframe with the read.table() command: butter <- read.table("butterflies.txt", header = TRUE) The data are in a plain text file where the first row gives the column names.
Add text annotation to ggplot code#
Make the packages you need available for use in this R Studio session by adding this code to your script and running it. Start R Studio and set your working directory to the folder you created:Īnd save it as figure.R or similar. Now get a copy of the data by saving this file the folder you just made. The first thing you want to do is make a folder on your computer where your code and the data for plotting will live. Some of the results of a statistical analysis are shown with annotation. In this example the data are the wing lengths for males and females of two imaginary species of butterfly in two regions, north and south.

I’ll also assume you’ve analysed your data so you know what annotations you want to add.įaceting is a very useful feature of ggplot which allows you to efficiently plot subsets of your data next to each other. I will assume you have R Studio installed and have at least a little experience with it but I’m aiming to make this do-able for novices. This is useful in its own right but can also help you understand ggplot better.
Add text annotation to ggplot how to#
In this post, I will show you how to add different annotations to each facet. This is a question I get fairly often and the answer is not straightforward especially for those that are relatively new to R and ggplot2. (with thanks to a student for sending me her attempt). Help! The same annotations go on every facet!
