【问题标题】:Error in ggplot Violin Graphs- trouble overlaying boxplot with violin [duplicate]ggplot Violin Graphs 中的错误 - 无法用小提琴覆盖箱线图 [重复]
【发布时间】:2021-05-13 15:52:15
【问题描述】:

我正在尝试将我的数据显示为带有重叠箱线图的小提琴图。我有四个组,被两个独立的因素分开,所以我输入了下面的命令。

该表有 X1、Category 和 Area 列。

ggplot(malbdata,aes(x=X1,y=Area,fill=Category))+  
       geom_violin()+
       geom_boxplot(width=.1)

我得到的是附上的图表,它把箱线图放在小提琴旁边,但不在小提琴里面。我对使用 R 很陌生;关于可能出现问题的任何想法?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我认为问题出在width = 0.1 参数上,例如

    library(tidyverse)
    library(palmerpenguins)
    
    penguins %>% 
      na.omit() %>%
      select(species, island, bill_length_mm) %>% 
      ggplot(aes(x = island, y = bill_length_mm, fill = species)) +
      geom_boxplot(width=.1) +
      geom_violin()
    

    如果您使宽度相同,它们会按预期排列:

    library(tidyverse)
    library(palmerpenguins)
    
    penguins %>% 
      na.omit() %>%
      select(species, island, bill_length_mm) %>% 
      ggplot(aes(x = island, y = bill_length_mm, fill = species)) +
      geom_boxplot(width=.2) +
      geom_violin(width=.2)
    

    此外,与其使用箱线图和小提琴(两者都说明值的分布),不如绘制单个值和分布,例如

    library(tidyverse)
    library(palmerpenguins)
    library(ggbeeswarm)
    
    penguins %>% 
      na.omit() %>%
      select(species, island, bill_length_mm) %>%
      rename(Species = species, Island = island) %>% 
      ggplot(aes(x = Island, y = bill_length_mm, fill = Species)) +
      geom_boxplot(width=.4, outlier.shape = NA,
                   position = position_dodge2(preserve = "single")) +
      geom_quasirandom(aes(colour = Species), groupOnX = TRUE,
                       width=.2, alpha = 0.5, dodge.width = 0.4) +
      theme_bw(base_size = 16) +
      ylab("Bill Length (mm)")
    

    【讨论】:

      猜你喜欢
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2022-12-09
      • 2015-01-16
      • 2016-07-02
      • 1970-01-01
      相关资源
      最近更新 更多