Add number of observations per group in ggplot2 boxplot - r

Following this question: How to add a number of observations per group and use group mean in ggplot2 boxplot?, I want to add number of observations per group in ggplot boxplot too. But I have added a colour into aes mapping.
The existing answer shows how to adjust text position in y axis. How could I adjust the text position in the x axis?
This is a minimum example to reproduce my problem:
library(ggplot2)
give.n <- function(x){
return(c(y = median(x)*1.05, label = length(x)))
# experiment with the multiplier to find the perfect position
}
p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median)
p
Thanks for any suggestions.

You can just use position:
p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p
The width argument of position_dodge() controls the positioning on the horizontal axis. 0.75 is the sweet spot, see how it works for different numbers of groupings:
p2 <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(cyl))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p2

Instead of stat_summary, you can use geom_text. Please refer to the following question: ggplot2 add text on top of boxplots.
This is an example of how you may do it with the number of observations:
# Create an aggregate of median & count
> cts <- merge(aggregate(mpg ~ cyl + am, mtcars, length),
aggregate(mpg ~ cyl + am, mtcars, median),
by=c("cyl", "am"))
# Rename the col names to fit with the original dataset..
> names(cts) <- c("cyl", "am", "count", "mpg")
# As alexwhan suggested, position_dodge helps with positioning
# along the x-axis..
> ggplot(mtcars, aes(factor(cyl), mpg, colour = factor(am))) +
geom_boxplot(position = position_dodge(width=1.0)) +
geom_text(data = cts, aes(label=count),
position=position_dodge(width=1.0))

Related

ggplot 2, boxplot add mean with legend

I would like to add for my ggplot boxplot a mean with a legend, but I don't know how.
The follwing R script is include in a function for a single variable:
p2 <- ggplot(df, aes("",!!sym(x))) +
geom_boxplot() +
stat_summary(fun=mean, geom="crossbar", color="steelblue2")
Thanks.
adapted from this solution Use stat_summary to annotate plot with number of observations you could try something like this:
fun_mean <- function(x) {return(data.frame(y = mean(x),
label = paste0("mean = ", round(mean(x), 2))))}
ggplot(mtcars, aes(factor(cyl), mpg, label = rownames(mtcars))) +
geom_boxplot(fill = "grey80", colour = "#3366FF") +
stat_summary(fun.data = fun_mean, geom = "text")
Hope that helps!

Centering dots in ggplot on faceted boxplot [duplicate]

Following this question: How to add a number of observations per group and use group mean in ggplot2 boxplot?, I want to add number of observations per group in ggplot boxplot too. But I have added a colour into aes mapping.
The existing answer shows how to adjust text position in y axis. How could I adjust the text position in the x axis?
This is a minimum example to reproduce my problem:
library(ggplot2)
give.n <- function(x){
return(c(y = median(x)*1.05, label = length(x)))
# experiment with the multiplier to find the perfect position
}
p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median)
p
Thanks for any suggestions.
You can just use position:
p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p
The width argument of position_dodge() controls the positioning on the horizontal axis. 0.75 is the sweet spot, see how it works for different numbers of groupings:
p2 <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(cyl))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p2
Instead of stat_summary, you can use geom_text. Please refer to the following question: ggplot2 add text on top of boxplots.
This is an example of how you may do it with the number of observations:
# Create an aggregate of median & count
> cts <- merge(aggregate(mpg ~ cyl + am, mtcars, length),
aggregate(mpg ~ cyl + am, mtcars, median),
by=c("cyl", "am"))
# Rename the col names to fit with the original dataset..
> names(cts) <- c("cyl", "am", "count", "mpg")
# As alexwhan suggested, position_dodge helps with positioning
# along the x-axis..
> ggplot(mtcars, aes(factor(cyl), mpg, colour = factor(am))) +
geom_boxplot(position = position_dodge(width=1.0)) +
geom_text(data = cts, aes(label=count),
position=position_dodge(width=1.0))

stat_summary bar chart labels [duplicate]

If I use ggplot2's stat_summary() to make a barplot of the average number of miles per gallon for 3-, 4-, and 5-geared cars, for example, how can I label each of the bars with the average value for mpg?
library(ggplot2)
CarPlot <- ggplot() +
stat_summary(data= mtcars,
aes(x = factor(gear),
y = mpg,
fill = factor(gear)
),
fun.y="mean",
geom="bar"
)
CarPlot
I know that you can normally use geom_text(), but I'm having trouble figuring out what to do in order to get the average value from stat_summary().
You should use the internal variable ..y.. to get the computed mean.
library(ggplot2)
CarPlot <- ggplot(data= mtcars) +
aes(x = factor(gear),
y = mpg)+
stat_summary(aes(fill = factor(gear)), fun.y=mean, geom="bar")+
stat_summary(aes(label=round(..y..,2)), fun.y=mean, geom="text", size=6,
vjust = -0.5)
CarPlot
but probably it is better to aggregate beforehand.
I'd simply precompute the statistics, and build the plot afterwards:
library(plyr)
library(ggplot2)
dat = ddply(mtcars, .(gear), summarise, mean_mpg = mean(mpg))
dat = within(dat, {
gear = factor(gear)
mean_mpg_string = sprintf('%0.1f', mean_mpg)
})
ggplot(dat, aes(x = gear, y = mean_mpg)) +
geom_bar(aes(fill = gear), stat = "identity") +
geom_text(aes(label = mean_mpg_string), vjust = -0.5)

Change stat_summary colours based on group and add text to the label in ggplot2 boxplot

Following this question: Add number of observations per group in ggplot2 boxplot, how do I change the colours of stat_summary?
Below is the example code:
give.n <- function(x){
return(c(y = median(x)*1.05, label = length(x)))
}
p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75))
p
So when I do the below to change the colours, it doesn't work (it gives me only 1 combined number)
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75), colour = c("black", "red"))
And when I do the below to add the text "n=" it doesn't work either. (I try to add the "n=" in the function itself, by doing the below:
give.n <- function(x){
return(c(y = median(x)*1.05, label = paste0("n=",length(x))))
}
But I get the below error:
Error: Discrete value supplied to continuous scale
For the colours, you want to add these using scale_colour_manual, so plot call looks like:
p <-
ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median,
position = position_dodge(width = 0.75)) +
scale_colour_manual(values = c("black", "red"))
The answer to adding "n=" is a duplicate of this question: Use stat_summary to annotate plot with number of observations. You need to use data.frame(...) in your give.n function rather than c(...):
give.n <-
function(x){
return(data.frame(y = median(x)*1.05, label = paste0("n=",length(x))))
}
EDIT:
Re comment on changing colours for stat_summary items only, this proved a bit tricky in that I don't think you can have multiple scale_colour_manual layers. However, in this case you can make use of the fill aesthetic for box plots and leave the colour aesthetic for your text geom.
To make it cleaner, I've taken the colour and fill aesthetics out of the ggplot(...) call and put these in each geom:
p <-
ggplot(mtcars, aes(factor(vs), mpg)) +
geom_boxplot(aes(fill = factor(am))) +
stat_summary(aes(colour = factor(am)), fun.data = give.n,
geom = "text", fun.y = median, position = position_dodge(width = 0.75)) +
scale_colour_manual(values = c("black", "red"))
Then if you want to specify colours for the box plot fill you can use scale_fill_manual(...)

Add a number of observations per group AND SUBGROUP in ggplot2 boxplot

This might seem like a duplicate of this question, but in fact I want to expand the original question.
I want to annote the boxplot with the number of observations per group AND SUBGROUP in ggplot. Following the example or the original post, here is my minimal example:
require(ggplot2)
give.n <- function(x){
return(c(y = median(x)*1.05, label = length(x)))
# experiment with the multiplier to find the perfect position
}
ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(gear))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median)
My problem is that the number of samples all line up in the center of the group, rather than plotting on the appropriate boxplot (as the picture below shows):
is it what you want?
require(ggplot2)
give.n <- function(x){
return(c(y = median(x)*1.05, label = length(x)))
# experiment with the multiplier to find the perfect position
}
ggplot(mtcars, aes(factor(cyl), mpg, fill = factor(gear))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median, position=position_dodge(width=0.75))

Resources