I have the following code. I'd like to change the color of the boxplots so they all have the same fill color (grey).
Also I'd like to have the stat_summary texts to stick to the bottom of each barplot but vjust only seem to provide relative position?
Thanks
boxp <- ggplot(mtcars, aes(as.factor(cyl), wt, fill=as.factor(am)) ) +
geom_bar(position = "dodge", stat = "summary", fun.y = "median") +
geom_boxplot(outlier.shape = NA, width=0.2, color = "black", position = position_dodge(0.9)) +
stat_summary(aes(label=round(..y..,2)), fun.y=median, geom="text", size=8, col = "white", vjust=8, position = position_dodge(0.9)) +
stat_summary(fun.y=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
boxp
Here is a possible solution, but it needs ggplot v3.3.0 for the stage() function.
To point out major changes:
Instead of using the fill as an implicit grouping, I've explicitly set the grouping so it isn't tied to the fill.
I added the fill as an aesthetic of the bar geom.
The boxplot now has the unmapped aesthetic fill = 'gray'
The text stat summary uses stage() to calculate the statistic but then uses 0 as actual placement.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(as.factor(cyl), wt,
group = interaction(as.factor(cyl), as.factor(am)))) +
geom_bar(aes(fill=as.factor(am)), position = "dodge", stat = "summary", fun = "median") +
geom_boxplot(outlier.shape = NA, width=0.2,
color = "black", fill = 'gray',
position = position_dodge(0.9)) +
stat_summary(aes(label=round(after_stat(y), 2), y = stage(wt, after_stat = 0)),
fun=median, geom="text", size=8, col = "white", vjust=-0.5,
position = position_dodge(0.9)) +
stat_summary(fun=mean, geom="point", shape=18, size=4, col="white", position = position_dodge(0.9)) +
labs(x = "Conditions", y = "Medians") +
scale_y_continuous(limits=c(0,7),oob = rescale_none) +
theme_bw()
Created on 2020-05-06 by the reprex package (v0.3.0)
Related
I am trying to use both coord_trans and coord_flip in the same plot, but that seems to not work. Any suggestion how to use coord_trans for a plot that needs to be flipped?
Using scale_y_log10 does not work since it messes up the stat_summary
p <- ggplot(df.2,aes(reorder(x,y),y,colour=z)) +
geom_jitter(width = 0.2,size=0.1) +
theme_classic(base_size = 8) +
stat_summary(
fun = mean,
geom = "errorbar",
aes(ymax = ..y.., ymin = ..y..),
position = position_dodge(width = 0.1),
width = 0.7,
colour="black") +
coord_flip() +
theme(legend.position = "none") +
labs(x="",y="") +
scale_color_manual(values = mycolors)
p + coord_trans(y = "log10")
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 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'm using the workaround to remove diagonal lines from a ggplot legend: https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/vJnF9_HBqx4
With the following data, how do I change the colours of the groups?
# Create data #
a<-as.data.frame(c(1,1,1,2,2))
b<-as.data.frame(c("A","A","B","B","A"))
c<-as.data.frame(c(20,20,60,50,50))
a<-cbind(a,b,c)
colnames(a)<-c("X","Gp","Y")
# Plot #
ggplot(a, aes(x=X, y=Y,fill=Gp)) +
geom_bar(stat = "identity", aes(colour = "black")) +
scale_color_identity() +
theme(legend.key = element_rect(colour = "black", size = 1))
I have tried changing the following elements :
scale_color_identity(values=c("red","yellow"))
geom_bar(stat = "identity", aes(colour = c("red","yellow")))
geom_bar(stat = "identity", aes(colour = "black"), fill=c("red","yellow"))
but each produces an error.
Try this. The guides call let's you pick which scale to not have a legend. And, you can set the outline colour without aes().
EDIT after comment about diagonal line in legend
Based on this SO question remove diagonal line in legend, you can added the guides(fill etc. call to remove the diagonal.
ggplot(a, aes(x=X, y=Y,fill=Gp)) +
geom_bar(stat = "identity", colour = "black") +
scale_fill_manual(values = c("red","yellow")) +
guides(fill = guide_legend(override.aes = list(colour = NULL))) +
guides(colour = FALSE)
You can also call geom_bar twice. Once for the legend, and without the color argument, and once with the color argument but suppressing the legend
ggplot(a, aes(x=X, y=Y,fill=Gp)) +
geom_bar(stat = "identity", color = 'black', show_guide = F) +
geom_bar(stat = 'identity') +
scale_fill_manual(values = c('red', 'yellow') )
I have a plot (below) that I am trying to control the legend for. The trouble is that I have both geom_line(aes(linetype = season)) and geom_bar(aes(linetype = season)). I'd like to display the legend for the geom_line (as in plot 1 below) rather than the legend for the geom_bar (as in plot 2 below).
If I try using scale_linetype(guide = F), it turns off the legend for both. Is there a way to display the legend for the geom_line linetype but not the geom_bar linetype? Below is the code to produce the plots.
Why? Ultimately I would like the outline of the bars to match the lines, with a nicely displayed legend. As you can see plot 2 is almost there but the legend is, well, illegible
# code for plot 1
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, fill = season),
stat = "identity", position = "dodge", color = "black") +
scale_fill_manual(values = c("white", "black"), guide = F)
# code for plot 2
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season),
stat = "identity", position = "dodge", color = "black") +
scale_fill_manual(values = c("white", "black"), guide = F)
Answered by joran below in a comment. Used show_guide in the geom_bar call, code shown below
# plot 3
ggplot() +
geom_line(data = temp, aes(pos, res, linetype = season)) +
geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season),
stat = "identity", position = "dodge", color = "black",
show_guide = F) +
scale_fill_manual(values = c("white", "black"))