【问题标题】:Automatic break after n-dots in geom_dotplot-function in ggplot in RR中ggplot中的geom_dotplot函数中的n点后自动中断
【发布时间】:2023-01-22 22:34:15
【问题描述】:

由于不同组之间的重叠,我在管理 geom_dotplot 函数中的多个观察时遇到了问题:

v1 <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
v2 <- c(0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
v3 <- c(13,67,89,280,40,1,23,99,32,1,75,280,270,200,196,300,320,277,23,4,1,2,5,89,45,23,11,1,3,23,100,100,100,100,100,200,100,11,6,6,123,100,100,100,100,100,12,86,11,300,75,100,110,19,299,100,100,100,100,100,100,100,100,11,100,120,110,100,100,300,300,250,100,100,100,12,100,100,75,5,10,10,10,10,10)

summary <- data.frame(v1, v2, v3)

summary$v1 <- as.factor(summary$v1)
summary$v2 <- as.factor(summary$v2)

ggplot(summary, aes(x = v1, y = v3, fill = v2)) + geom_boxplot(width = 0.5, position = position_dodge(0.75)) + geom_dotplot(binaxis  = "y", stackdir = "center", binwidth = 3.25, position = position_dodge(0.75))

图片示例:https://i.stack.imgur.com/eLmee.png

我考虑过手动更改数据,目的是最多只有 5 个具有相同值的观察值(例如 v3 <- (... 100, 100, 100, 100, 100, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 130, ...))。但是,它也会影响箱线图的结果(中位数、四分位间距)。

我找不到任何选项可以在 5 个点后自动中断以不重叠。也许,有一个简单而巧妙的解决方案。感谢您的所有帮助。先感谢您!

【问题讨论】:

    标签: r ggplot2 dot-plot


    【解决方案1】:

    您可以制作一个较小的数据集,其中每组只包含五个观察值(summary2 下面)。您可以使用原始数据制作方框,使用较小的数据制作点。

    library(dplyr)
    library(ggplot2)  
    v1 <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
    v2 <- c(0,0,0,0,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
    v3 <- c(13,67,89,280,40,1,23,99,32,1,75,280,270,200,196,300,320,277,23,4,1,2,5,89,45,23,11,1,3,23,100,100,100,100,100,200,100,11,6,6,123,100,100,100,100,100,12,86,11,300,75,100,110,19,299,100,100,100,100,100,100,100,100,11,100,120,110,100,100,300,300,250,100,100,100,12,100,100,75,5,10,10,10,10,10)
    
    summary <- data.frame(v1, v2, v3)
    
    summary$v1 <- as.factor(summary$v1)
    summary$v2 <- as.factor(summary$v2)
    summary2 <- summary %>% 
      group_by(v1, v2, v3) %>% 
      filter(1:n() <= 5)
    
    
    
    ggplot() + 
      geom_boxplot(data = summary, aes(x = v1, y = v3, fill = v2), width = 0.5, position = position_dodge(0.75)) + 
      geom_dotplot(data = summary2, aes(x = v1, y = v3, fill = v2), binaxis  = "y", stackdir = "center", binwidth = 3.25, 
                   position = position_dodge(0.75))
    

    reprex package (v2.0.1) 创建于 2023-01-22

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      相关资源
      最近更新 更多