Plot multiple boxplot in one graph
You should get your data in a specific format by melting your data (see below for how melted data looks like) before you plot. Otherwise, what you have done seems to be okay.
require(reshape2)
df <- read.csv("TestData.csv", header=T)
# melting by "Label". `melt is from the reshape2 package.
# do ?melt to see what other things it can do (you will surely need it)
df.m <- melt(df, id.var = "Label")
> df.m # pasting some rows of the melted data.frame
# Label variable value
# 1 Good F1 0.64778924
# 2 Good F1 0.54608791
# 3 Good F1 0.46134200
# 4 Good F1 0.79421221
# 5 Good F1 0.56919951
# 6 Good F1 0.73568570
# 7 Good F1 0.65094207
# 8 Good F1 0.45749702
# 9 Good F1 0.80861929
# 10 Good F1 0.67310067
# 11 Good F1 0.68781739
# 12 Good F1 0.47009455
# 13 Good F1 0.95859182
# 14 Good F1 1.00000000
# 15 Good F1 0.46908343
# 16 Bad F1 0.57875528
# 17 Bad F1 0.28938046
# 18 Bad F1 0.68511766
require(ggplot2)
ggplot(data = df.m, aes(x=variable, y=value)) + geom_boxplot(aes(fill=Label))
Edit: I realise that you might need to facet. Here's an implementation of that as well:
p <- ggplot(data = df.m, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=Label))
p + facet_wrap( ~ variable, scales="free")
Edit 2: How to add x-labels
, y-labels
, title
, change legend heading
, add a jitter
?
p <- ggplot(data = df.m, aes(x=variable, y=value))
p <- p + geom_boxplot(aes(fill=Label))
p <- p + geom_jitter()
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p
Edit 3: How to align geom_point()
points to the center of box-plot? It could be done using position_dodge
. This should work.
require(ggplot2)
p <- ggplot(data = df.m, aes(x=variable, y=value))
p <- p + geom_boxplot(aes(fill = Label))
# if you want color for points replace group with colour=Label
p <- p + geom_point(aes(y=value, group=Label), position = position_dodge(width=0.75))
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p
how to create multiple boxplots from the same dataframe?
Using ggplot::facet_wrap()
makes it easy to make a bunch of panels of a graph from one dataset. However for it to work the variable that defines the different panels has to be in a single column. In this case that means you have to get from the 'wide' format to a 'long' format of your data. For this I suggest tidyr::pivot_longer()
. Last point is that you need to treat your label
as a factor
otherwise you won't get separate boxes as you do in base R because I think that converts the x variable into a factor by default which {ggplot2} won't do (although it will give an informative warning). Finally, if you want to have separate y
axes for each plot you can set scales = "free_y"
inside facet_wrap()
.
library(tidyverse)
df <- data.frame(var_1 = c(1,2,3,4,5,6,7,8,9),
var_2 = c(21,23,34,45,23,56,76,54,65),
var_3 = c(6,5,4,3,5,7,3,2,5),
label = c(1,1,1,2,1,2,2,1,2))
df %>%
pivot_longer(-label) %>%
ggplot(aes(factor(label), value)) +
geom_boxplot() +
facet_wrap(vars(name), nrow = 1)
Created on 2022-02-12 by the reprex package (v2.0.1)
How to plot multiple boxplots with a single variable each on ggplot2?
Bring your data in long format with
pivot_longer
fromtidyr
package (is intidyverse
)use
ggplot
fromggplot2
package (is also intidyverse
)geom_boxplot
andgeom_jitter
if needed.
library(tidyverse)
df %>%
mutate(id = row_number()) %>%
pivot_longer(
cols = starts_with("X"),
names_to = "names",
values_to = "values"
) %>%
ggplot(aes(x=names, y=values, fill=names))+
geom_boxplot() +
geom_jitter(aes(y=values))
Draw a boxplot with several boxplots using ggplot2
You can try melting the data using reshape2
library.
Here's how you can do this:
library(reshape2)
ggplot(melt(df), aes(x = variable, y = value)) +
geom_boxplot()
The output will look like this:
Multiple boxplots placed side by side for different column values in ggplot
Your problem is that you need a long format to do facet_wraps.
#first, reshape to long
library(reshape2)
df1_long <- melt(df1, id.vars=c("ID","Tool","Name"))
#then plot
p2 <- ggplot(df1_long, aes(x=factor(Tool),y=value,fill=factor(Tool)))+
geom_boxplot() + labs(title="CMP") +facet_wrap(~variable)
p2
Multiple boxplot for different variable using same facet_wrap
Do you mean like this? A tribble
by the way is a nice way to create a minimal sample of data.
library(tidyverse)
tribble(
~participant, ~memory, ~attention, ~language, ~executive, ~cognitive,
"A", 2, 5, 2, 2, 0,
"B", 2, 2, 5, 2, 1,
"C", 2, 2, 2, 2, 0,
"D", 2, 3, 2, 6, 1,
"E", 2, 2, 2, 2, 0,
"F", 2, 2, 8, 2, 0,
"G", 2, 4, 2, 2, 1,
"H", 2, 2, 7, 2, 1
) |>
pivot_longer(c(memory, attention, language, executive),
names_to = "domain", values_to = "score") |>
ggplot(aes(domain, score)) +
geom_boxplot() +
facet_wrap(~cognitive) +
theme_bw() +
coord_flip() +
labs(
title = "Cognitive domains in baseline groups",
y = "Z score"
)
Created on 2022-04-20 by the reprex package (v2.0.1)
How to plot multiple boxplot in one graph for R 2020
What's wrong with just using boxplot
? No ggplot2
, and that should work in your version, too.
However, it's not unambiguous what you mean by "multiple box plots in one graph". Here three versions:
## by social group
op <- par(mfrow=c(1, 2)) ## set par
boxplot(Absenteeism.time.in.hours ~ Social.smoker, dat)
boxplot(Absenteeism.time.in.hours ~ Social.drinker, dat)
par(op) ## reset par
## by social group in one panel
datl <- reshape(dat, varying=2:3, direction="long")
boxplot(Absenteeism.time.in.hours ~ time + Social, datl)
## social group interaction
boxplot(Absenteeism.time.in.hours ~ ., dat)
Data:
dat <- read.table(header=T, text=" Absenteeism.time.in.hours Social.smoker Social.drinker
1 4 0 1
2 0 0 1
3 2 0 1
4 4 1 1
5 2 0 1
6 2 0 1
7 8 0 1
8 4 0 1
9 40 0 1
10 8 0 0
11 8 0 1
12 8 0 1
13 8 0 1
14 1 0 1
15 4 0 1
16 8 0 1
17 2 0 1
18 8 1 1
19 8 0 0
20 2 1 0")
using a loop to plot multiple boxplots in ggplot2, long dataframe
Split the data for each Analyte
and use map
to save the plot as separate image.
library(tidyverse)
df %>%
group_split(Analyte) %>%
map(~{
analyte_name <- .$Analyte[1]
tmp <- ggplot(., aes(FY, Value)) + geom_boxplot() + ggtitle(analyte_name)
ggsave(paste0(analyte_name, '.png'), tmp)
})
Related Topics
Shiny App: Nothing Changes When Clicking on Action Button
Installing R 3.5.0 with --Enable-R-Shlib
How to Get a Warning on "Shiny App Will Not Work If the Same Output Is Used Twice"
R Install Package Loaded Namespace
How to Do Conditional Grouping of Data in R
Convert Comma Separated String to Integer in R
R: How to Draw a Line with Multiple Arrows in It
How to Remove Na from Facet_Wrap in Ggplot2
Interpolate/Extend Quarterly to Monthly Series
How to Run Lm Regression for Every Column in R
Geom_Col Is Assigning the Wrong Independent Variable