Draw Multiple Boxplots in One Graph in R Side-by-Side (4 Examples) | Base, ggplot2 & lattice Package
HTML-код
- Опубликовано: 9 сен 2024
- How to plot several boxplots in the same graphic in the R programming language. More details: statisticsglob...
R code of this video:
set.seed(75829547) # Create example data
data <- data.frame(A = rnorm(1000),
B = runif(1000),
C = rpois(1000, 3))
boxplot(data) # Applying boxplot function
install.packages("reshape2") # Install reshape2 package
library("reshape2") # Load reshape2
data_long <- melt(data) # Reshaping data frame
install.packages("ggplot2") # Install ggplot2 package
library("ggplot2") # Load ggplot2
ggplot(data_long, aes(x = variable, y = value)) + # Applying ggplot function
geom_boxplot()
install.packages("lattice") # Install lattice package
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
install.packages("ggplot2") # Install ggplot2 package
library("ggplot2") # Load ggplot2
ggplot(iris_long, aes(x = variable, y = value, color = Species)) + # Applying ggplot function
geom_boxplot()
Follow me on Social Media:
Facebook: / statisticsglobecom
Patreon: / statisticsglobe
Pinterest: www.pinterest....
Reddit: / joachimschork
Twitter: / joachimschork
You are so good with teaching, it's mind blowing. Thank you saving us soooo much time!!!
Thank you very much for your positive feedback! 🙂
Thank you so much this was so useful I’ve been looking all day for this :)
Thanks a lot for your kind feedback. Glad it helped! 🙂
Excellent, this code was also an opportunity to install (& load) some indispensable packages in my freshly re-installed R/R-Studio :-D
Thanks AG! Great to hear that your R settings work well again :)
@@StatisticsGlobe Thanks, this will help me greatly with some projects. Also, the new spell checker is dope!
you explain so perfect. Thank you very much indeed
Hi Tina, thank you very much for the nice words! It's great to hear that the tutorials are helpful!
Thanks for this helpful video clip
Hey Suman, thanks for the feedback! Great to hear that the video was helpful!
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
Hey Mercedeh, thank you for the kind comment, glad you like the video. Regarding your question, I think there are some problems when you are creating your data. Please have a look at the modified code below:
xxx
@@StatisticsGlobe Thank you!
You are very welcome Mercedeh!
Thank you !!!
You're very welcome Amal!
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
Hello Mebruta,
I am not sure if I got you well. Do you like to mark the boxplots for each treatment and each body weight after testing the difference between the treatment for each body weight? What kind of labeling are you planning for? I couldn't get it well. Could you please describe it a bit further?
Regards,
Cansu
@@cansustatisticsglobe Yes. I want to describe the response variables for each treatment group by boxplot, including the SEM for each variable. Thanks
@@mebratuify So, you want to label each boxplot with the standard error of the mean?
Regards,
Cansu
@@cansustatisticsglobe Yes. Do you have an email, please? Thanks
Hello @@mebratuify,
Unfortunately, I don't provide my email address. I have written a sample code that might be useful. I stuck with the first case that you provided. So I used significant subscription letters showing the significance of the analysis. And for the sake of efficiency, I have only used bodyweight 1 and bodyweight 2. Please see the code below.
set.seed(123) # for reproducibility
# Create a data frame with three treatments, each with 8 replications
df
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
Hello!
I couldn't get what you mean by compact letter display and iris boxplot? Could you please elaborate on that?
Best
Cansu
The video gave a two way boxplot using data on iris. How can I add letters to show significant differences. I can email some data for the demo if you share an email.
Hey @@igy7913 !
You can do the following.
# Reshape iris to long format
iris_long
@@cansustatisticsglobe Thanks a million!
Welcome @@igy7913 !
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
Hi Dung, have you had a look at the patchwork package already? It allows you to draw multiple plots side by side. More info: statisticsglobe.com/patchwork-r-package Regards, Joachim
@@StatisticsGlobe
Thanks Joachim for showing patchwork packages. I had a look at it, but the video did not show time series. My question are: How to plot (1) line plot combined with (2) Standard deviation plot with the time series. For instant: (1) line plot showing the simulated daily crop yield (kg) from 01.01.2020 -31.12.2010 AND (2) Standard deviation plot is the observed data showing just observed data for some days, each day we have 05 replicated values (e.g. 15.01.2020, 30.04.2020, and 30.07.2020) during the simulated time 2020. How to organize the data to plot the 02 plots in one figure?
I'm sorry mate, but it is very difficult to explain how to create an entire multi-plot. Maybe this thread helps? stackoverflow.com/questions/26020142/adding-shade-to-r-lineplot-denotes-standard-error Regards, Joachim
👍👍👍
Thanks Nganja!
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.
Hey Darwin, thank you very much for the kind words! You may change the fill colors manually as shown below:
ggplot(iris_long, aes(x = variable, y = value, fill = Species)) +
geom_boxplot() +
scale_fill_manual(breaks = unique(iris_long$Species),
values = c("red", "blue", "green"))
it is possible to make a boxplot graph together with a line graph. Having two y axes?
Hey Maurícia, this is actually a very controversial topic and the ggplot2 package restricts the usage of two y-axes (more info here: stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales). That being said, you may have a look at the following tutorials. They do not do exactly what you are asking for, but maybe they are still useful to you: statisticsglobe.com/draw-ggplot2-plot-with-two-y-axes-in-r statisticsglobe.com/r-draw-plot-with-two-y-axes Regards, Joachim
@@StatisticsGlobe thank you !!
You are very welcome! :)
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
Hey Ahmad, thanks for the kind words, glad you like the video! Regarding your question, this really depends on how you want to visualize your data. How should the y-axis be displayed? Maybe it would be a good option to zoom into some parts of your plot? This tutorial explains how to do that in case of barplots: statisticsglobe.com/r-ggplot2-barplot-with-break-and-zoom-in-axis Regards, Joachim
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
Hey Dalila, thanks for the nice comment. I think you cannot attach graphics to RUclips comments, though. Could you share your question in the Statistics Globe Facebook group? You would be able to attach graphs there, and others could contribute/read as well: facebook.com/groups/statisticsglobe
@@StatisticsGlobe Ok, I did forward my question and attached a picture on Statistics Globe. For some reason, I just messaged the picture. Thank you!
I just saw in the group that you found a solution. Glad to hear that! :)
@@StatisticsGlobe Yes:)1 Thank you so much! I am a beginner in R statistics, I am still not fully confident to handle some R issues (even simple) in a given time frame. My skills in R programming have developed a lot though compared to where I started a few months ago :). Simplified Tutorials like yours have helped a lot.
This is really great to hear! I wish you the very best for your further progress in R programming! :)
Thanks for this great video
How can I sort the plots in each group by the mean?
Thanks
Hey Moyo, thank you for the kind comment and the very interesting question. It has inspired me to create a new tutorial on how to sort boxplots by the median value. You can find it here: statisticsglobe.com/sort-boxplot-median-r Regards, Joachim
Your prompt response is greatly appreciated.
I'll love to have you as my mentor on R, I'm fairly familiar with the use of python for data analysis and visualisation. However, I am attempting a switch to R. Seems it has simple solutions to some very complex problems in python.
Thanks a lot for the very kind feedback Moyo, glad it was helpful!