ggplot geom_jitter behind (multiple) geom_boxplot [duplicate] - r

This question already has answers here:
ggplot2 - jitter and position dodge together
(2 answers)
Closed 5 years ago.
i use following code:
data(mtcars)
ggplot(mtcars, aes(x=factor(cyl), y=mpg)) +
geom_jitter(aes(colour=factor(gear)), width = 0.1) +
geom_boxplot(aes(fill=factor(gear)), alpha=0.6)
with following result:
But i want the colored dots from geom_jitter directly behind the corresponding(!) boxplot. Is there a way to do it?

Solution is position_jitterdodge as mentioned by aosmith and his link.
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(x=factor(cyl), y=mpg, fill=factor(gear), colour=factor(gear))) +
geom_point(position = position_jitterdodge()) +
geom_boxplot(alpha=0.6)
The result looks like:

Related

Different `geom_hline()` for each facet of ggplot [duplicate]

This question already has an answer here:
Display a summary line per facet rather than overall
(1 answer)
Closed 4 years ago.
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg$hwy))
I want each geom_hline() in the facet shown above to be the mean of the points that are only contained within that facet. I would think that I could do it with something like (below). But that doesn't work. I'm close, right?
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg %>% group_by(year, fl)$hwy))
If you have the value you wish to use for each facet as a column in the data frame, and that value is unique within each facet, then you can use geom_hline(aes(yintercept=column)), which will then plot a horizontal line for each of the facets

How to place a text on a free position in a ggplot2 plot [duplicate]

This question already has answers here:
How to position annotate text in the blank area of facet ggplot
(2 answers)
Closed 4 years ago.
If I use the following code
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~cyl, ncol = 2)
p
I obviously get a chart with one blank facet in the SE corner (resulting chart). I'd like to place some text into this blank facet providing e.g. an explanation or any other information pertinent to the chart. I haven't yet found a solution, all solutions place the text in the filled facets.
Thanks for your help!
Ulrich
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_wrap(~cyl, ncol = 2)
p
library(grid)
grid.text("your text", x = 0.75, y = 0.25)
see the answer that i got here

How to avoid overlapping in ggplot2 boxplot? [duplicate]

This question already has an answer here:
Spacing between boxplots in ggplot2
(1 answer)
Closed 6 years ago.
I want to plot the boxplot:
library("ggplot2")
p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot(aes(colour = drv))
but the boxplot showed overlapped within each class.
How can I add distance between boxes?
just try following variant
library("ggplot2")
dodge <- position_dodge(width = 0.9)
p <- ggplot(mpg, aes(class, hwy))
p+geom_boxplot(aes(fill = drv),position=dodge)
With position=dodge you can set the distance between boxplots
Hope this helps
Best
Pavlo

Combined box-violin plot not aligned [duplicate]

This question already has an answer here:
Align violin plots with dodged box plots
(1 answer)
Closed 7 years ago.
I want to graph a distribution along two dimensions using a violinplot with a boxplot in it. The result can be really fascinating, but only when done right.
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
plot <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_violin() + geom_boxplot(width=0.1) + theme(legend.position="none")
ggsave(filename="Violinboxplot.png", plot, height=6, width=4)
This is however what I get:
The boxplots are aligned along the axis belonging to the factor. How can I shift them to be in the center of the violinplots?
There is an answer to this question here:
how to align violin plots with boxplots
You can use the position argument to shift the graph elements as needed:
dodge <- position_dodge(width = 0.5)
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_violin(position = dodge) +
geom_boxplot(width=.1, position = dodge) +
theme(legend.position="none")

How to change the width of the middle line in box whisker plot in ggplot2 in R? [duplicate]

This question already has an answer here:
change thickness median line geom_boxplot()
(1 answer)
Closed 9 years ago.
In the current version of ggplot2, the middle line in a box-whisker-plot is drawn bold compared to the other lines:
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot()
I think this was not the case in some older versions. Is there a way to separately adjust the width of the middle line?
Poorly documented, but you can use the fatten argument in geom_boxplot
library(ggplot2)
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(fatten = 0)
p + geom_boxplot(fatten = 4)

Resources