I've plotted the mean for the whole study population (black line) and for men and women separately.
plotYYIR1<- ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
labs(x="Week number", y="YYIR1 distance run (m)") +
theme(plot.title = element_text(hjust = 0, vjust=0))+
theme(legend.title=element_blank())+
theme(legend.key.width = unit(1, "cm"))+
stat_summary(fun.y = mean,geom = "point", size=2) +
stat_summary(fun.y = mean, geom = "line", size=0.7) +
stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) +
scale_shape_manual(values = c("Male"=17, "Female"=15))+
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) +
scale_colour_manual(values = c("#009CEF", "#CC0000"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2)+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex))
plotYYIR1
The legend only shows the genders, could someone help me with adding the black line and points in the legend for the whole group?
You need to add aes() to get a legend for the black line/points. If you want the legend to be for lines/shapes combined you can turn off the legend for shapes by adding guide = F to scale_shape_manual and then use override.aes in guides to specify the shapes in the legend:
ggplot(data=YYIR1Long, aes(x=TimeValue, y=YYIR1Value)) +
labs(x="Week number", y="YYIR1 distance run (m)") +
theme(plot.title = element_text(hjust = 0, vjust=0))+
theme(legend.title=element_blank())+
theme(legend.key.width = unit(1, "cm"))+
stat_summary(fun.y = mean,geom = "point", size=2, aes(colour = "mean")) +
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour = "mean")) +
stat_summary(fun.y = mean,geom = "point", size=2, aes(shape=Sex,colour=Sex)) +
scale_shape_manual(values = c("Male"=17, "Female"=15, "mean"=16), guide = F)+
stat_summary(fun.y = mean, geom = "line", size=0.7, aes(colour=Sex)) +
scale_colour_manual(values = c("#009CEF", "#CC0000", "#000000"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour = "mean"))+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width =2, aes(colour=Sex)) +
guides(colour = guide_legend(override.aes = list(shape = c("Male"=17, "Female"=15, "mean"=16))))
Related
ggplot(data =data,aes(x = Voting.Method, y = Accuracy, linetype =Type))+
geom_boxplot(fill="white", fatten = 0) +
stat_summary(fun = mean, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "mean"),
width = .75, linetype = "solid") +
stat_summary(fun = median, geom = "errorbar", aes(ymax = ..y.., ymin = ..y.., color = "median"),
width = .75, linetype = "solid") +
theme(legend.title=element_blank(), legend.text = element_text(size=10, face="bold"),
legend.position =c(0.9,0.9),legend.direction = "horizontal") +
scale_y_continuous(limits = c(50, 75))
i want to move one legend to bottom such like that
I want to annotate mean of each boxplot using ggplot2. However, I could not figure out how to horizontally center the symbols marking the means within their respective boxes (see image below).
MWE is below for reference:
library(ggplot2)
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv)) +
theme_bw()
Try with position_dodge()
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv),
position = position_dodge(width = .75)) +
theme_bw()
I have eg data and syntax for a scatter (jitter) plot below
eg_data <- data.frame(
period = c(sample( c("1 + 2"), 1000, replace = TRUE)),
max_sales = c(sample( c(1,2,3,4,5,6,7,8,9,10), 1000, replace = TRUE, prob =
c(.20, .10, .15, .20, .15, .10, .05, .02, .02, .01))) )
jitter <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y..), colour = "red", size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y..), colour = "gold", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y..), colour = "blue", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y..), colour = "black", size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y..), colour = "green", size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter
I cannot find documentation on how to define a legend for the horiztonal quantile / mean lines I have in this graph.
How to add legend to ggplot manually? - R
I came across this SO question / answer but I wasn't able to implement it, when I include color inside the aes setting, it doesn't work.
EDIT - a member suggested I add color to the aes specification...here is the same graph with color and size included.
jitter2 <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y.., colour = "red"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "gold"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "blue"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "black"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "green"), size = 1) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
labs(fill = "Period") )
jitter2
So...any help is appreciated. Thank you!
I found the answer to my own question.
jitter <- (
(ggplot(data = eg_data, aes(x=period, y=max_sales)) +
geom_jitter(stat = "identity", width = .15, color = "blue", alpha = .4)) +
scale_y_continuous(breaks= seq(0,12, by=1)) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.25)), geom = "hline", aes(yintercept = ..y.., colour = "25%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.50)), geom = "hline", aes(yintercept = ..y.., colour = "50%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.75)), geom = "hline", aes(yintercept = ..y.., colour = "75%"), size = 1) +
stat_summary(fun.y = "quantile", fun.args = list(probs = c(0.90)), geom = "hline", aes(yintercept = ..y.., colour = "90%"), size = 1) +
stat_summary(fun.y = "mean", geom = "hline", aes(yintercept = ..y.., colour = "mean"), size = 1.5) +
ggtitle("Max Sales x Period 1 and 2") + xlab("Period") + ylab("Sales") +
theme(plot.title = element_text(color = "black", size = 14, face = "bold", hjust = 0.5),
axis.title.x = element_text(color = "black", size = 12, face = "bold"),
axis.title.y = element_text(color = "black", size = 12, face = "bold")) +
scale_colour_manual(values = c("red", "blue", "gold", "green", "black"), name = "Percentiles"))
jitter
Also, quickly, the idea of "just use (something), everyone gets it" as a suggestion is not helpful, and assumes way too much about the final intended audience. First time I've ever posted a question and had that as a reply. I asked a specific question for a specific reason.
I know that there are many questions already open to add significance level in ggplot2 in faceted plots. However, as a R beginner, I did not found a solution for my plot.
My data (.txt file) is available in this link:
1drv.ms/t/s!AsLAxDXdkA9Mg8oXdJ-qxD5AeB4KAw
There are four columns: three factor levels (temperature, parasitoid species and behavior) and a numeric level (number of parasitism and host-killing).
I run the plot with the code:
ggplot(mydata, aes(x = temperature, y = value, fill = species)) +
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = "dodge", stat="identity") +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size = 11, face = "italic"))
Now I want to add the significance level in each plot, comparing separately the results in Parasitism and Host-Killing in both temperatures. So I have 6 contrasts in each plot. I tested the option using the function stat_compare_means. However, as advised in this Rblogger tutorial (https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/) I should make a list telling what I want to compare. But, in this case, I have two results for 25°C and two for 30°C. How I could create this list mentioning all the contrasts?
If anybody could clarify for me how I can solve this, it would help me a lot. Thanks.
stat_compare_means worked "out of the box" (which makes sense, since it's using the grouping defined in the ggplot function.
ggplot(mydata, aes(x = temp, y = value, fill = factor(species))) +
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size = 11, face = "italic")) +
stat_compare_means(method = "t.test")
Alternatively, switch the x and fill variables:
ggplot(mydata, aes(x = factor(species), y = value, fill = factor(temp))) +
facet_grid(. ~ behavior) +
stat_summary(fun.y = mean, geom = "bar", position = position_dodge(width = 1), stat="identity") +
stat_summary(fun.data = mean_sdl, geom="errorbar", position = position_dodge(width = 1), width=0.25, stat="identity", fun.args = list(mult = 1)) +
labs(x = "Temperature", y = "Nº of parasitized or host-killed larvae") +
theme(legend.position = "bottom", legend.text = element_text(size = 11, face = "italic")) +
stat_compare_means(method = "t.test")
Thanks #thc for your help and patience. As I decided to use letters over the error bars, I had to create a new data.frame for each letter and them add the geom_text. This got it right with the below code, after testing many x and y values to insert the letters in the right point. But certainly it has an easier way to do it that I don't know it.
sigvals1 <- data.frame(x=0.75,y=222.4, text=c("a"), behavior=c("Parasitism"))
sigvals2 <- data.frame(x=1.207,y=190.55, text=c("a"), behavior=c("Parasitism"))
sigvals3 <- data.frame(x=1.75,y=117.7, text=c("b"), behavior=c("Parasitism"))
sigvals4 <- data.frame(x=2.209,y=103, text=c("b"), behavior=c("Parasitism"))
sigvals5 <-data.frame(x = 0.75, y = 74.8, text=c("A"), behavior = c("Host-Killing"))
sigvals6 <-data.frame(x = 1.20, y = 92.97, text=c("B"), behavior = c("Host-Killing"))
sigvals7 <-data.frame(x = 1.74, y = 49.8, text=c("C"), behavior = c("Host-Killing"))
sigvals8 <-data.frame(x = 2.196, y = 43, text=c("C"), behavior = c("Host-Killing"))
plot +
geom_text(data=sigvals1, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals2, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals3, aes(x=x,y=y, label=text, fill=NA), hjust=0)+
geom_text(data=sigvals4, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals5, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals6, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals7, aes(x=x,y=y, label=text, fill=NA), hjust=0) +
geom_text(data=sigvals8, aes(x=x,y=y, label=text, fill=NA), hjust=0)
I want to annotate mean of each boxplot using ggplot2. However, I could not figure out how to horizontally center the symbols marking the means within their respective boxes (see image below).
MWE is below for reference:
library(ggplot2)
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv)) +
theme_bw()
Try with position_dodge()
ggplot(data=mpg, mapping=aes(x=class, y=hwy)) +
geom_boxplot(aes(color = drv), outlier.shape = NA) +
stat_summary(fun.y = mean, geom = "point", size=2, aes(shape = drv, color = drv),
position = position_dodge(width = .75)) +
theme_bw()