[英]Plot 3 boxplots into one plot
Having three dataframes like this:拥有三个这样的数据框:
Dataframe 1: Dataframe 1:
df1 <- structure(list(company = structure(c(3L, 5L, 1L, 2L, 4L, 3L,
5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L,
1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L,
2L, 4L), .Label = c("amazon", "bsd", "google", "so", "yahoo"), class = "factor"),
period = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L), .Label = c("after", "before"), class = "factor"), val = c(0.262776250810038,
0.187917588433778, 0.697682733346741, 0.158756228911086,
0.378985944448169, 0.249033541149918, 0.157828875332395,
0.762575137985743, 0.148767625304462, 0.394985586914259,
0.268776116734822, 0.177604969721347, 0.694811289133204,
0.160510379656321, 0.389823691090702, 0.280675292172242,
0.181169135885232, 0.655493731983643, 0.177839601349691,
0.387633795892829, 0.257949543026971, 0.169661013161717,
0.665359433308753, 0.149795535295301, 0.384002592120846,
0.244474983799245, 0.162231011597506, 0.650253625617304,
0.147493910750598, 0.424582690889589, 0.291490692945409,
0.241190141002436, 0.622555920538089, 0.215134857321624,
0.383108757346205, 0.25750262563965, 0.230989251636835, 0.708699246944202,
0.193749860338316, 0.427264195213515)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -40L))
Second:第二:
df2 <- structure(list(company = structure(c(3L, 5L, 1L, 2L, 4L, 3L,
5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L,
1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L,
2L, 4L), .Label = c("amazon", "bsd", "google", "so", "yahoo"), class = "factor"),
period = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L), .Label = c("after", "before"), class = "factor"), val = c(0.262776250810038,
0.187917588433778, 0.697682733346741, 0.158756228911086,
0.378985944448169, 0.249033541149918, 0.157828875332395,
0.762575137985743, 0.148767625304462, 0.394985586914259,
0.268776116734822, 0.177604969721347, 0.694811289133204,
0.160510379656321, 0.389823691090702, 0.280675292172242,
0.181169135885232, 0.655493731983643, 0.177839601349691,
0.387633795892829, 0.257949543026971, 0.169661013161717,
0.665359433308753, 0.149795535295301, 0.384002592120846,
0.244474983799245, 0.162231011597506, 0.650253625617304,
0.147493910750598, 0.424582690889589, 0.291490692945409,
0.241190141002436, 0.622555920538089, 0.215134857321624,
0.383108757346205, 0.25750262563965, 0.230989251636835, 0.708699246944202,
0.193749860338316, 0.427264195213515)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -40L))
Third:第三:
df3 <- structure(list(company = structure(c(3L, 5L, 1L, 2L, 4L, 3L,
5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L,
1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L, 2L, 4L, 3L, 5L, 1L,
2L, 4L), .Label = c("amazon", "bsd", "google", "so", "yahoo"), class = "factor"),
period = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L), .Label = c("after", "before"), class = "factor"), val = c(0.262776250810038,
0.187917588433778, 0.697682733346741, 0.158756228911086,
0.378985944448169, 0.249033541149918, 0.157828875332395,
0.762575137985743, 0.148767625304462, 0.394985586914259,
0.268776116734822, 0.177604969721347, 0.694811289133204,
0.160510379656321, 0.389823691090702, 0.280675292172242,
0.181169135885232, 0.655493731983643, 0.177839601349691,
0.387633795892829, 0.257949543026971, 0.169661013161717,
0.665359433308753, 0.149795535295301, 0.384002592120846,
0.244474983799245, 0.162231011597506, 0.650253625617304,
0.147493910750598, 0.424582690889589, 0.291490692945409,
0.241190141002436, 0.622555920538089, 0.215134857321624,
0.383108757346205, 0.25750262563965, 0.230989251636835, 0.708699246944202,
0.193749860338316, 0.427264195213515)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -40L))
We can create a boxplot for every data frame using this:我们可以使用以下方法为每个数据框创建一个箱线图:
library(stringr)
library(ggplot2)
df1 = pivot_longer(df1, everything())
df2 = cbind(df1,str_split_fixed(df1$name,"_",2))
colnames(df2)[3:4]=c("Company","Time")
ggplot(df2, aes(x=Company, y=value, fill=Time)) + geom_boxplot() + coord_flip()
How is it possible to have the box plots of the three dataframes in the same plot next to other and have the same y-axis (only one time) and have title that "this it df1", "this it df2" and "this it df3"?怎么可能在同一个 plot 中拥有三个数据帧的箱形图,并且具有相同的 y 轴(只有一次)并且标题为“this it df1”、“this it df2”和“this它是 df3"? x-axis will be 3 time. x 轴将是 3 次。
You can use the package ggpubr to achieve it.您可以使用 package ggpubr 来实现它。 Like this:像这样:
library(ggplot2)
library(ggpubr)
colnames(df1) <- c('Company','Time','Value')
colnames(df2) <- c('Company','Time','Value')
colnames(df3) <- c('Company','Time','Value')
graf1 <- ggplot(df1,aes(x=Company, y=Value,fill=Time)) +
geom_boxplot() +
ggtitle('Dataset 1') +
guides(fill=FALSE)
graf2 <- ggplot(df2,aes(x=Company, y=Value,fill=Time)) +
geom_boxplot() +
ggtitle('Dataset 2') +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
guides(fill=FALSE)
graf3 <- ggplot(df3,aes(x=Company, y=Value,fill=Time)) +
geom_boxplot() +
ggtitle('Dataset 3') +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
ggarrange(graf1,graf2,graf3,nrow = 1,ncol = 3)
How about with a facet_wrap
?用facet_wrap
怎么样?
First we'll combine all the data into one nice data.frame
with bind_rows
, setting a new column based on the data.frame
name with the .id =
argument.首先,我们将使用bind_rows
将所有数据合并到一个漂亮的data.frame
中,并根据带有.id =
参数的data.frame
名称设置一个新列。
Then we'll plot the combined data and use facet_wrap
to split by the new DF
factor.然后我们将 plot 组合数据并使用facet_wrap
按新的DF
因子拆分。
library(dplyr)
library(ggplot2)
combined.df <- bind_rows(df1,df2,df3,.id = "DF") %>%
mutate(DF = paste0("DataFrame_",DF))
ggplot(combined.df, aes(x=company, y=val)) +
geom_boxplot(aes(fill = period)) + coord_flip() +
facet_wrap(~ DF)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.