【问题标题】:Side by side geom_boxplot for the high and low values of multiple variables in R?R中多个变量的高值和低值并排显示geom_boxplot?
【发布时间】:2020-01-22 22:24:13
【问题描述】:

我正在尝试创建boxplot,它将在多个位置(即开始、中间、结束)用于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)

这是我想要实现的手动绘制的图形

【问题讨论】:

    标签: r ggplot2 boxplot facet-wrap gridextra


    【解决方案1】:

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

    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"))
    

    回应您的评论,我并不热衷于将 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))
    

    【讨论】:

    • 非常感谢 - 这太棒了。是否可以为HighLow 设置一个面板,而不是每个面板分成三部分。此外,我不想要轴的文本(即 Ob、A、B),而是我想要 legends key 引用不同的 variables
    猜你喜欢
    • 2020-11-12
    • 2022-09-23
    • 1970-01-01
    • 2013-06-05
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2013-07-17
    相关资源
    最近更新 更多