ggplot2 使用 gghalves + ggdist 的雨云图无法正确绘制数据?

问题描述 投票:0回答:1

我很可能在这里遗漏了一些明显的东西,但我开始认为这可能是一个错误(?)我已经研究了很长时间了。我正在使用 ggplot2、gghalves、ggdist 绘制雨云图。我正在研究一些鸟类每天在筑巢空洞的平均独特竞争者可能会受到一对繁殖年份的影响(在我的专栏繁殖者状态中分为一次性繁殖者与重复繁殖者),推断重复繁殖者有一些来自某处的某种优势。我得到了原始数据,其中一些半眼显示了它们的分布,然后是一个汇总统计线。

我注意到,每次重新运行此图时,一些点值都会发生变化(实质上?),但它们绝对与数据集不匹配。值显示一些随机的东西,但我的数据集中不存在。此时我已经尝试了几乎所有的方法。抖动设置是我认为可能的情况 - 但似乎并非如此。简化绘图,删除 coord_flip() 函数,更改 range_scale 值,更新 r 和所有包 - 等等。

这是我的数据片段,它似乎仍在为我做这件事:

data <- tibble::tribble(
  ~nobroods, ~avg_uniq_comp, ~hollow_breeder_status,
        4,    0.1500000,             "repeat",
        3,    0.2666667,             "repeat",
        1,    0.6000000,           "one-time",
        1,    0.6000000,           "one-time",
        3,    0.2666667,             "repeat",
        1,    0.2000000,           "one-time",
        2,    0.3000000,             "repeat",
        2,    0.4000000,             "repeat",
        2,    0.4000000,             "repeat",
        1,    0.4000000,           "one-time"
)

这是使用 ggplot、gghalves、ggdist 和 oli_hawkins 试点主题包的代码 (library(pilot) #remotes::install_github("olihawkins/pilot")

uniquecompplot <- ggplot(data, 
                         aes(x = hollow_breeder_status, 
                             y = avg_uniq_comp,
                             colour = hollow_breeder_status, 
                             fill = hollow_breeder_status)) +
    gghalves::geom_half_point(aes(shape = as.factor(nobroods), 
                                colour = "Summary statistics"),
                           side = "l", #choose right or left side 
                            range_scale = 0.05, # spread of points
                            alpha = .6,
                            size = 2.2,
                            position = position_identity()) +
  ggdist::stat_halfeye(
    adjust = .4,           # smoothness of distribution
    width = .87,           # height of distribution
    colour = NA) +
  labs(title = "Breeder status' relationship to unique competitors per day",
       subtitle = "Summary statistics: general linear model results over raw data",
       x = "Breeder status",
       y = "Average unique competitors per day") +
  coord_flip() +
  pilot::scale_color_pilot() +
  pilot::scale_fill_pilot() +
  pilot::theme_pilot(grid = "",
                     axes = "b") + 
  theme(legend.position = "bottom",
        axis.text.y = element_text(face = "italic"),
        plot.title = element_text(size = 16),  # Adjust title size
        plot.subtitle = element_text(size = 12)) + # Adjust subtitle size 
  scale_shape_manual(name = "Number of broods",  # Add legend for Nobroods
                     values = c("1" = 16, "2" = 17, "3" = 15, "4" = 8)) + # Specify shapes manually
  guides(colour = FALSE, fill = FALSE) +
  scale_color_manual(name = NULL,  # Add legend for boxplot colours
                     values = c("Summary statistics" = "black")) +  # Specify the custom color  
  lims(y = range(seconddataun$avg_uniq_comp))  # Set the y-axis limits to match the extent of your data

uniquecompplot

我已经陷入了一切的深渊,却找不到解决方案。

ggplot2 visualization ggdist
1个回答
0
投票

剥离代码来隔离问题,绘图

p1
捕获了默认转换
position_jitter()
产生的视觉变化。

使用

position_identity()
代替,
p2
每次运行都会给出相同的结果。

library(tidyverse)
library(gghalves)

data <- tribble(
  ~nobroods, ~avg_comp, ~status,
  4, 0.1500000, "repeat",
  3, 0.2666667, "repeat",
  1, 0.6000000, "one-time",
  1, 0.6000000, "one-time",
  3, 0.2666667, "repeat",
  1, 0.2000000, "one-time",
  2, 0.3000000, "repeat",
  2, 0.4000000, "repeat",
  2, 0.4000000, "repeat",
  1, 0.4000000, "one-time"
)

# 2 runs with position_jitter()
p1 <- ggplot(data, aes(status, avg_comp, colour = status, fill = status)) +
  geom_half_point(aes(shape = as.factor(nobroods)))

p1

p1


# 2 runs with position_identity()
p2 <- ggplot(data, aes(status, avg_comp, colour = status, fill = status)) +
  geom_half_point(aes(shape = as.factor(nobroods)),
                  transformation = position_identity())

p2

p2

创建于 2024-04-06,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.