-
-
Save timedreamer/e3154e2653513c659fbe1131423c6fca to your computer and use it in GitHub Desktop.
quick example of a (not ugly) ggplot bar graph with points for individual subjects and error bars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example of bar plot with individual subject points for Anya -04/24/2019 | |
# added error bars - 04/25/2019 | |
library(tidyverse) #will need to install this first (run: install.packages("tidyverse")) | |
## fake data | |
data = tibble( # creating a dataframe (aka "tibble") called data | |
subject = rep(1:10,times = 2 ), # making a column/vector of subject numbers (1-10) x2 | |
condition = rep(c("hard", "easy"), each = 10), # making a column/vector of condition names | |
response = c(rnorm(10, mean = 5, sd = 1), rnorm(10, mean = 3, sd = 1)) # randomly generating responses from normal distributions with different means | |
) | |
## quick and dirty plot for looking at your data in the privacy of your laptop | |
basic_plot = ggplot(data)+ # essential first piece where you "set the stage" | |
# first layer here is going to take the mean of the y values and draw columns with those means as their height | |
stat_summary(mapping = aes(x = condition, y = response, fill = condition), geom = 'col', fun.y = 'mean', color = 'black')+ | |
# second layer will just plot each response value without doing any kind of transformation | |
geom_point(mapping = aes(x = condition, y = response, fill = condition), shape = 21, size = 3) | |
basic_plot # voila, you have a basic plot! | |
## nicer plot you can put in papers | |
better_plot = ggplot(data)+ | |
stat_summary(mapping = aes(x = condition, y = response, fill = condition), geom = 'col', fun.y = 'mean', color = 'black')+ | |
#adding jitter to avoid overplotting | |
geom_point(mapping = aes(x = condition, y = response, fill = condition), shape = 21, size = 3, position = position_jitter(width = 0.2, height=0))+ | |
#adding error bars (standard error) | |
stat_summary(mapping = aes(x = condition, y = response), geom = 'errorbar', fun.data = 'mean_se', color = 'black',size = 2, width=0)+ | |
#after doing the same basic/important stuff as in the plot above, you cn start tweaking the aesthetics | |
scale_fill_brewer(palette = "BuGn", name = NULL, breaks = c("easy","hard"), labels = c("Easy", "Hard"))+ # changes the color palette and makes the legend labels nicer | |
labs(x = NULL, y = "% BOLD response")+ # changes the y axis label, removes x axis label | |
theme_classic(base_size = 20)+ # changes the overall style of the plot | |
theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank()) # adjusting further aspects of the plot, removing unneccessary tick marks, etc. | |
better_plot | |
#save plot when you're happy with it | |
ggsave("<filename.pdf>", plot = better_plot) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment