Preserve Width & Position of Single ggplot2 Boxplot in R (2 Examples) | position_dodge() Function

preview_player
Показать описание
R code of this video:

subgroup = letters[1:2],
value = rnorm(90))
data$subgroup[data$group == "A" & data$subgroup == "b"] <- "a"

library("ggplot2") # Load ggplot2

ggplot(data, # Default ggplot2 boxplot
aes(x = group,
y = value,
fill = subgroup)) +
geom_boxplot()

ggplot(data, # Using position_dodge2 function
aes(x = group,
y = value,
fill = subgroup)) +
geom_boxplot(position = position_dodge2(preserve = "single"))

ggplot(data, # Using position_dodge function
aes(x = group,
y = value,
fill = subgroup)) +
geom_boxplot(position = position_dodge(preserve = "single"))

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Thank you very much Joachim, your videos are an immense help!

mrhsabari
Автор

Hello Joachim, Thaks for the time and effor you put into making these videos. It makes life very easy for most entry level R programmers.

I have a question: what does the fill argument inside ggplot(aes(fill = subgroup)) do ? I see that it adds a legend and helps in categoring the variables by colour, but I also see that it doesnt work for Barplots and some other kind of plots

vallepereira
Автор

Hey, i am encountering the same problem and while preserve = single helps a lot there is also an issue that seems much more difficult to solve. the preserve = single automatically aligns your bars to the left. If in your case a red bar would be missing it would also align the sole blue bar to the left, something you don't like to see. Now doing this in position_dodge2() it will align all your bars centrally which is a small work around but not perfect. Do you have any ideas on solving those specific cases?
Otherwise great explanation. Keep up the good work!

sanderwessels
Автор

I do not know why mine is appear flat lines instead of boxes?

tchistermorrelebissa
Автор

Could we see the data from Excel till imported?

tchistermorrelebissa
Автор

Is ggplot a kind of software? R incorported ggplot2?

tchistermorrelebissa
join shbcf.ru