Last active
March 11, 2022 16:16
-
-
Save andreagrioni/10e1c44dfddf7eb2bc20e525fa8b4974 to your computer and use it in GitHub Desktop.
create line plot of RFU distribution across groups at different timepoints
This file contains 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
# from http://www.sthda.com/english/wiki/ggplot2-line-plot-quick-start-guide-r-software-and-data-visualization | |
#+++++++++++++++++++++++++ | |
# Function to calculate the mean and the standard deviation | |
# for each group | |
#+++++++++++++++++++++++++ | |
# data : a data frame | |
# varname : the name of a column containing the variable | |
#to be summariezed | |
# groupnames : vector of column names to be used as | |
# grouping variables | |
data_summary <- function(data, varname, groupnames){ | |
require(plyr) | |
summary_func <- function(x, col){ | |
c(mean = mean(x[[col]], na.rm=TRUE), | |
sd = sd(x[[col]], na.rm=TRUE)) | |
} | |
data_sum<-ddply(data, groupnames, .fun=summary_func, | |
varname) | |
data_sum <- rename(data_sum, c("mean" = varname)) | |
return(data_sum) | |
} | |
# generate mean and sd by group | |
test_table_stats <- data_summary( | |
test_table, | |
varname="q_norm", | |
groupnames = c("rash_level", "timepoint") | |
) | |
# Use position_dodge to move overlapped errorbars horizontally | |
ggplot( | |
test_table_stats, | |
aes(x=timepoint, | |
y=q_norm, | |
group=rash_level, | |
color=rash_level) | |
) + | |
geom_errorbar(aes(ymin=q_norm-sd, ymax=q_norm+sd), width=.1, | |
position=position_dodge(0.05)) + | |
geom_line() + | |
geom_point()+ | |
scale_color_manual(values = c("None" = "grey", "Grade1" = "orange", "Grade2" = "red")) + | |
theme_minimal() + | |
ggplot2::ylab("RFU") + | |
ggplot2::ggtitle("test") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment