r - R中多个变量的高值和低值并排显示geom_boxplot?

标签 r ggplot2 boxplot facet-wrap gridextra

我正在尝试创建 boxplot 来比较 obAB 在多个位置(即, Start, Mid, End) 用于 TopLow 值(在本例中为 10%)。我正在尝试在 R< 中使用 gatherfacet_wrapgrid.arrangeggplot 功能 但不能把东西放在一起。到目前为止,这是我的代码 - 我将不胜感激。

library(tidyverse)
library(gridExtra)

DF_1 = data.frame(Ob = runif(100, 10,80), A = runif(100, 5, 90), B = runif(100, 3,85), loc = rep("Start",100))
DF_2 = data.frame(Ob = runif(100, 10,80), A = runif(100, 5, 90), B = runif(100, 3,85), loc = rep("Mid",100))
DF_3 = data.frame(Ob = runif(100, 10,80), A = runif(100, 5, 90), B = runif(100, 3,85), loc = rep("End",100))

DF_1_Top = DF_1[order(DF_1$Ob,decreasing = TRUE),][1:10,]
DF_1_Low = DF_1[order(DF_1$Ob,decreasing = FALSE),][1:10,]

DF_2_Top = DF_2[order(DF_2$Ob,decreasing = TRUE),][1:10,]
DF_2_Low = DF_2[order(DF_2$Ob,decreasing = FALSE),][1:10,]

DF_3_Top = DF_1[order(DF_3$Ob,decreasing = TRUE),][1:10,]
DF_3_Low = DF_1[order(DF_3$Ob,decreasing = FALSE),][1:10,]

DF_Top = rbind(DF_1_Top, DF_2_Top, DF_3_Top)
DF_Low = rbind(DF_1_Low, DF_2_Low, DF_3_Low)

DF_T = gather(DF_Top, key = "Variable", value = "Value", - "loc")
DF_L = gather(DF_Low, key = "Variable", value = "Value", - "loc")

P1 = ggplot(DF_T, aes(x = Variable, y = Value))+
geom_boxplot()+facet_wrap(~loc, nrow = 1)

P2 = ggplot(DF_L, aes(x = Variable, y = Value))+
  geom_boxplot()+facet_wrap(~loc, nrow = 1)

grid.arrange(P1,P2, nrow = 2)

这是我想要实现的手动绘制的图形 enter image description here

最佳答案

您可以将所有数据堆叠到一个数据框中并创建一个图表。例如:

d = bind_rows(High=DF_Top, Low=DF_Low, .id='source') %>% 
  mutate(source=factor(source, levels=c("High","Low")))

d %>% 
  gather(key, value, Ob:B) %>% 
  mutate(key = fct_relevel(key, "Ob")) %>% 
  ggplot(aes(key, value)) +
    geom_hline(yintercept=0) +
    geom_boxplot() +
    facet_grid(source ~ loc, switch="x") +
    labs(x="", y="") +
    scale_y_continuous(expand=expand_scale(mult=c(0.0, 0.02))) +
    theme_classic() +
    theme(strip.placement="outside", 
          strip.background.x=element_rect(colour=NA, fill=NA),
          strip.text.x=element_text(size=11, face="bold"))

enter image description here

回应您的评论,我并不热衷于将 key 标签移动到图例,但是...

d %>% 
  gather(key, value, Ob:B) %>% 
  mutate(key = fct_relevel(key, "Ob")) %>% 
  ggplot(aes(loc, value, colour=key)) +
    geom_hline(yintercept=0) +
    geom_boxplot() +
    facet_grid(source ~ ., switch="x") +
    labs(x="", y="", colour="") +
    scale_y_continuous(expand=expand_scale(mult=c(0.0, 0.02))) +
    theme_classic() +
    theme(legend.position="bottom",
          legend.box.margin=margin(t=-20))

enter image description here

关于r - R中多个变量的高值和低值并排显示geom_boxplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59869123/

相关文章:

r - 为什么 R 没有按正确的顺序对我的 tibble 进行排序?

r - 对两个具有交换行/列的不同数据帧进行 t 检验?

r - 如何在ggplot2中修改箱线图的晶须?

read.csv直接进入R中的字符向量

r - 如何返回预期的图形(ggplot2,r中的椭圆)?

r - 如何使用引导值注释 ggtree 对象?

r - 在 Rmarkdown 中动态创建选项卡对 ggplot 不起作用,而对 plotly 起作用

r - 向水平箱线图添加点

r - 向箱线图添加颜色 - "Continuous value supplied to discrete scale"错误

R - 将许多 1D 格式的时间序列转换为 3D 数组,每个时间序列标记有两个标签