简体   繁体   English

R Plot 箱线图来自一个 plot 中的不同数据框

[英]R Plot boxplots from different Dataframes in one plot

I´m about to analyze some data and stuck with the visualization and can´t get any progress right now.我即将分析一些数据并坚持可视化,现在无法取得任何进展。

So, here are dummy dataframes which are similar to the ones I use:所以,这里有一些类似于我使用的虚拟数据帧:

df1<-data.frame(replicate(15,sample(0:200,1500,rep=TRUE)))
df2<-data.frame(replicate(15,sample(0:200,1500,rep=TRUE)))
df3<-data.frame(replicate(36,sample(0:200,1500,rep=TRUE)))
df4<-data.frame(replicate(9,sample(0:200,1500,rep=TRUE)))

And the problem is the following :问题如下

I want to plot Boxplots of each Dataframe as a whole besides each other: So that the boxplots for df1, df2, df3 and df4 are besides each other in one plot.我想将 plot 每个 Dataframe 的箱线图作为一个整体彼此相辅相成:以便 df1、df2、df3 和 df4 的箱线图在一个 Z32FA6E1B78A96D4024CZAA2E 中彼此相邻。 I don´t wanna have each station but the dataframe as a whole in this boxplot.我不想在这个箱线图中有每个站,但 dataframe 是一个整体。

The boxplots for each dataframe works smoothly:每个 dataframe 的箱线图工作顺利:

boxplot(df1, las=2)
boxplot(df2, las=2)
boxplot(df3, las=2)
boxplot(df4, las=2)

I then tried to combine them ggplot:然后我尝试将它们组合在一起 ggplot:

ggplot(data = NULL, aes(x, y))+
  geom_boxplot(data = df1, aes())+
  geom_boxplot(data = df2, aes())+
  geom_boxplot(data = df3, aes())+
  geom_boxplot(data = df4, aes())

But here i get a error message但是在这里我收到一条错误消息

Fehler in FUN(X[[i]], ...): Objekt 'x' nicht gefunden Fehler in FUN(X[[i]], ...): Objekt 'x' nicht gefunden

that something is wrong with the aes(), which is obvious, but i don´t have an idea what i can choose for x & y. aes() 有问题,这很明显,但我不知道我可以为 x 和 y 选择什么。 Maybe i just think in a too complicated way, but yeah...there´s some link I´m missing.也许我只是以一种过于复杂的方式思考,但是是的......我缺少一些链接。

So i hope everything is understandable and if information is missing then just ask and i add it!所以我希望一切都是可以理解的,如果信息缺失,那么只要问我就会添加它!

Maybe this is what you are looking for.也许这就是你要找的。 To replicate the base R boxplots via ggplot2 you could要通过 ggplot2 复制基本 R 箱线图,您可以

  1. Put your df's in a list把你的df放在一个列表中
  2. Convert the df's to long format for which I use lapply and a helper function which将 df 转换为我使用lapply的长格式和一个助手 function
    • converts the df to long format using tidyr::pivot_longer使用tidyr::pivot_longer将 df 转换为长格式
    • use forcats::fct_inorder to convert column with the variable names to a factor and preserves the right order as in the original df.使用forcats::fct_inorder将具有变量名称的列转换为因子并保留原始 df 中的正确顺序。
  3. Bind the long df's into one dataframe using eg dplyr::bind_rows where I add an id variable使用例如dplyr::bind_rows将长 df 绑定到一个 dataframe 中,其中我添加了一个 id 变量
  4. After the data wrangling it's an easy task to make boxplots via ggplot2 whereby I opted for facetting by df.在数据争吵之后,通过 ggplot2 制作箱线图是一项简单的任务,因此我选择了 df 刻面。
library(ggplot2)
library(tidyr)
library(dplyr)

df1<-data.frame(replicate(15,sample(0:200,1500,rep=TRUE)))
df2<-data.frame(replicate(15,sample(0:200,1500,rep=TRUE)))
df3<-data.frame(replicate(36,sample(0:200,1500,rep=TRUE)))
df4<-data.frame(replicate(9,sample(0:200,1500,rep=TRUE)))

df <- list(df1, df2, df3, df4)

to_long <- function(x) {
  pivot_longer(x, everything()) %>% 
    mutate(name = forcats::fct_inorder(name))
}
df <- lapply(df, to_long)
df <- bind_rows(df, .id = "id")

ggplot(df, aes(name, value)) +
  geom_boxplot() +
  facet_wrap(~id, scales = "free_x")

EDIT To get a boxplot for all columns of a dataframe and the boxplots side-by-side you can do编辑要获得 dataframe 的所有列的箱线图和并排的箱线图,您可以

ggplot(df, aes(id, value)) +
  geom_boxplot()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM