Draw Multiple Boxplots in One Graph in R Side-by-Side (4 Examples) | Base, ggplot2 & lattice Package

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

B = runif(1000),
C = rpois(1000, 3))

boxplot(data) # Applying boxplot function

library("reshape2") # Load reshape2

data_long <- melt(data) # Reshaping data frame

library("ggplot2") # Load ggplot2

ggplot(data_long, aes(x = variable, y = value)) + # Applying ggplot function
geom_boxplot()

library("lattice") # Load lattice package

bwplot(value ~ variable, data_long) # Applying bwplot function

data(iris) # Loading iris flower data set

iris_long <- melt(iris, id = "Species") # Reshaping iris data

library("ggplot2") # Load ggplot2

ggplot(iris_long, aes(x = variable, y = value, color = Species)) + # Applying ggplot function
geom_boxplot()

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

You are so good with teaching, it's mind blowing. Thank you saving us soooo much time!!!

natashalekhuleni
Автор

Thank you so much this was so useful I’ve been looking all day for this :)

ChenuliSooriyaarachchi
Автор

Excellent, this code was also an opportunity to install (& load) some indispensable packages in my freshly re-installed R/R-Studio :-D

agsoutas
Автор

you explain so perfect. Thank you very much indeed

tinAbraham_Indy
Автор

Thanks for the explanation. I didn't get the Lattice one! for example when I have the following set of data:
par(mai = c(1, 1, 1, 1), omi = c(0, 0, 0, 0))
set.seed(591)
xx1 <- rnorm(20, mean = 3, sd = 3.6)
xx2 <- rpois(40, lambda = 3.5)
xx3 <- rchisq(31, df = 5, ncp = 0)
box1 <- boxplot(xx1, xx2, xx3, names = c("Group-1", "Group-2",
"Group-3"), cex = 0.7)

and I want to recreate this boxplot using lattice, I should write it as follow?:
set.seed(591)
xxx <- data.frame(xx1 <- rnorm(20, mean = 3, sd = 3.6),
xx2 <- rpois(40, lambda = 3.5),
xx3 <- rchisq(31, df = 5, ncp = 0))
print("xxx Dataframe")
xxx

bwplot(value ~ xxx, data = xx1, xx2, xx3, main = "all groups")
#I get an error and it is not the correct format, I appreciate it if you can tell me how it should be.


thank you.

mercedehsasanpour
Автор

Thank you for your service. In my case, I have three treatments/groups. Examples, CON treat, ANT treat, and ADD Treat. Each has 8 replications. Th variables include Body weight 1, Body wight 2 and body weight 3. How can do a box plot with significant subscription letters (like a, ab, or b...etc). Thanks

mebratuify
Автор

Thanks for the video. Do you mind demonstrating how to add compact letter display for the iris boxplot, which has two factors and a response variable. Thanks

igy
Автор

Thank you very much! Your work is amazing. Is it possible to change to color of the boxplots? scale_fill_manual(breaks = data$x, values = c("black", "white", "black", "white") is not working for multiple boxplots.

darwinwoodley
Автор

it is possible to make a boxplot graph together with a line graph. Having two y axes?

mauriciarocha
Автор

Hi Globe.
Thanks. Your Video is very useful.
Could you pls. show me how to plot 02 graphs in the same figure as follows:
- Y axis is Kg
- X axis is time series (date)
Plot1 : line showing simulated crop yield in kg corresponding to the time series
Plot2: is the Standard deviation showing measured data at some specific date (e.g. 01 March, 15 April, 30 May, and 20 June) within the time series. Each measured date we measure 5 replicate time. It means we have 05 valuse to calculate standard deviation.
Plot 1 and 2 in the same figure.
Thanks you in advance,
Best,
Lam

quanglam
Автор

Thanks for the helpful video. Referring to your last plot with subgroups, I created a similar one but since I have only two sub groups, the spacing between the subgroups is huge. So I was wondering how can I get the two subgroups a lil bit closer to eachother? In other words, if I were to reduce the space between the segal length and the sepal width sub groups in your last plot, how would I do that? TIA

ahmadabdurrehman
Автор

Hello Mr. Joachim! I am stuck at a plot where I need to use stat_ellipse to get some variables plated: one of my issues in building the code is I don't know why the subset function is not working in stat_ellipse graph style.
Plot4<-ggplot(data = subset(QP1_norm, SyllableCount=="2"& (vowels=='i'|vowels=='æ')& syllableType=='o'& PreceedingConsonant2=='yes')) +
ggplot(QP1_norm, aes(x=f2, y=f1, color=VowelPosition))+
geom_point(aes(x=f2, y=f1), alpha = 0.3) +
stat_ellipse(level = 0.67, geom = "polygon", alpha = 0.1, aes(fill =(vowels=='i')))+
facet_wrap(vowels~condition) +
labs(x='F2', y='F1')+
ggtitle('Difference in vowel quality(F1 and F2) across syllable positions in disyllbaic words')+
theme(plot.title = element_text(hjust = 0.5))+theme(plot.title = element_text(face="bold"))
Plot4

I would like to get something that looks like this attached manually drawn graph

Your guidance is much appreciated!!

dalga
Автор

Thanks for this great video
How can I sort the plots in each group by the mean?
Thanks

moxieable