Changing whisker end in geom_boxplot
Adapted from the answer Changing whisker definition in geom_boxplot
p <- ggplot(data=concentration,aes(factor(location), formaldehyde),ylim=c(0,0.15),cex.axis=1.5,cex.lab=15)
f <- function(x) {
r <- quantile(x, probs = c(0, 0.25, 0.5, 0.75, 1))
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
p + stat_summary(fun.data=f, aes(fill= Condition), geom="boxplot", position="dodge")
Add horizontal bars to whiskers on percentile based boxplot
Since you have already calculated the values for the ends of the whiskers you can use geom_errorbar()
directly rather than via stat_boxplot()
as in the link you gave.
You will need to explicitly dodge the error bars to match the default dodging of the boxplots.
The required aesthetics for geom_errobar()
are ymin
and ymax
. I put these within the geom_errorbar()
layer. since you use these for both the boxplots and the errorbars you could move them up to the global aes()
to avoid repetition.
ggplot(data = new_df, aes(x = Group, group = Type, fill = Type)) +
geom_errorbar(aes(ymin = Percentile_0,
ymax = Percentile_100),
width = 0.5,
position = position_dodge(width = 0.9) ) +
geom_boxplot(
stat = "identity",
aes(
ymin = Percentile_0,
lower = Percentile_25,
middle = Percentile_50,
upper = Percentile_75,
ymax = Percentile_100
)
) +
theme_classic()
Label whiskers on ggplot boxplot when there are outliers
Boxplots use boxplots.stats
. You can directly use this in your stat_summary
:
ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(cyl))) +
geom_boxplot(width=0.6) +
stat_summary(
aes(label=sprintf("%1.1f", ..y..), color=factor(cyl)),
geom="text",
fun.y = function(y) boxplot.stats(y)$stats,
position=position_nudge(x=0.33),
size=3.5) +
theme_bw()
If you only need the whiskers, simply use boxplot.stats(y)$stats[c(1, 5)]
instead.
How to modify whiskers of a boxplot in ggplot2?
How about plotting two boxplots on top of each other. One with red lines and a second one on top without any wiskers at all.
p + geom_boxplot(color="red") + geom_boxplot(aes(ymin=..lower.., ymax=..upper..))
Changing whisker length of multiple boxplot in R
By default (notched=FALSE), the geom_boxplot() should give you the whisker you want (Q1 - 1.5*IQR / Q3 + 1.5*IQR). See a more current question link. Although, this is subjected to the quantile, IQR definition.
If you insist on setting them manually with stat_summary
# geom_boxplot parameters with stat summary
f <- function(x) {
r <- quantile(x, probs = c(0.25, 0.25, 0.5, 0.75, 0.75))
r[[1]]<-r[[1]]-1.5*IQR(x) #ymin lower whisker, as per geom_boxplot
r[[5]]<-r[[5]]+1.5*IQR(x) #ymax upper whisker
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
# To subset the outlying points for plotting,
o <- function(x) {
r <- quantile(x, probs = c(0.25, 0.75))
r[[1]]<-r[[1]]-1.5*IQR(x)
r[[2]]<-r[[2]]+1.5*IQR(x)
subset(x, x < r[[1]] | r[[2]] < x)
}
# added seed for consistency
set.seed(123)
df <- data.frame(matrix(rnorm(2000), ncol = 10))
plot.data <- gather(df, variable, value)
# plot.data$out <- as.numeric(rep(input_data, each = nrow(x_train)))
p <- ggplot(plot.data, aes(x = 0, y=value))
p <- p + stat_summary(fun.data = f, geom="boxplot")+
stat_summary(fun.y = o, geom="point")
#p <- p + geom_point(aes(x = 0, y = test_data), color = "red")
p <- p + facet_wrap(~variable, scales = "free_x", strip.position = 'top', ncol = 2)
p <- p + coord_flip()
p <- p + xlab("") + ylab("")
p <- p + theme(legend.position="none") + theme_bw()
p <- p + theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank())
Changing whisker definition in facet'ed geom_boxplot
You could use stat_summary
to customize the appearance, e.g.
ggplot(dfmelt, aes(x=bin, y=value, fill=variable)) +
stat_summary(geom = "boxplot",
fun.data = function(x) setNames(quantile(x, c(0.05, 0.25, 0.5, 0.75, 0.95)), c("ymin", "lower", "middle", "upper", "ymax")),
position = "dodge")
Related Topics
Deleting Rows That Are Duplicated in One Column Based on the Conditions of Another Column
How to Swap Columns Around in a Data Frame Using R
Keeping Zero Count Combinations When Aggregating with Data.Table
Forcing R Output to Be Scientific Notation with at Most Two Decimals
Excel Cell Coloring Using Xlsx
How to Generate Bin Frequency Table in R
R Dpylr Select_If with Multiple Conditions
How to Extend '==' Behavior to Vectors That Include Nas
Add Vline to Existing Plot and Have It Appear in Ggplot2 Legend
How to Plot the Relative Proportions of Two Groups Using a Fill Aesthetic in Ggplot2
R Shiny: Download Existing File
Rm(List=Ls()) Doesn't Completely Clear the Workspace
R Shiny Mouseover Text for Table Columns
R: Numeric 'Envir' Arg Not of Length One in Predict()
How to Split a Data Frame by Rows, and Then Process the Blocks
R Shiny Checkboxgroupinput - Select All Checkboxes by Click