gpt4 book ai didi

r - 如何改变 "position_dodge"一个点而不是 ggplot2 系列中的其他点

转载 作者:行者123 更新时间:2023-12-04 14:03:39 27 4
gpt4 key购买 nike

问题:我的数据集有一个共享基线(时间点 1)和 2 个重复测量。后续点在 2 个不同的条件下重复(即交叉)。绘图时,结果、误差线和数据点重叠。

library(tidyverse)
set.seed(11)
df_test <-
tibble(group = factor(rep(c("A", "B"), each = 3)),
timepoint = factor(rep(1:3, 2)),
y = c(0, rnorm(2, mean = 0, sd = .2), 0, rnorm(2, mean = .7, sd = 0.35))) %>%
mutate(ymax = y + .2,
ymin = y - .2)

# plot_nododge <-
df_test %>%
ggplot(aes(timepoint, y,
group = group,
shape = group,
fill = group)) +
geom_linerange(linetype = 1,
aes(ymin = ymin,
ymax = ymax)) +
geom_point() +
geom_line()

使用 position_dodge() 的解决方案: 该解决方案修复了重叠,但“基线”点实际上是一个单独的度量。我希望这是一个单一的点,以避免混淆,但仍然避开后续点。

有没有我缺少的简单解决方案?

我想为每个点使用自定义闪避(例如 scale_position_dodge_identity),但位置不被接受为美学。

# plot1 <- 
df_test %>%
ggplot(aes(timepoint, y,
group = group,
shape = group,
fill = group)) +
geom_linerange(linetype = 1,
position = position_dodge(.2),
aes(ymin = ymin,
ymax = ymax)) +
geom_point(position = position_dodge(.2)) +
geom_line(position = position_dodge(.2))

解决与扩展*也许将来会开发出更好的东西,但现在手动调整位置似乎是最好的。由于点的形状和颜色也应该反射(reflect)不同的“基线”度量,并且 x 轴应该有很好的标签,所以我进行了编辑以显示更多的收尾工作。

df_test %>% 
mutate(shape_var = case_when(timepoint == 1 ~ 22,
group == "A" ~ 21,
group == "B" ~ 24),
fill_var = case_when(timepoint == 1 ~ "black",
group == "A" ~ "red",
group == "B" ~ "blue"),
timepoint2 = as.numeric(as.character(timepoint)),
timepoint2 = timepoint2 + 0.05*(timepoint2 > 1 & group == "B"),
timepoint2 = timepoint2 - 0.05*(timepoint2 > 1 & group == "A")) %>%
ggplot(aes(timepoint2, y,
group = group,
shape = shape_var,
fill = fill_var)) +
geom_linerange(aes(ymin = ymin,
ymax = ymax)) +
geom_line() +
geom_point(size = 2) +
scale_x_continuous(breaks = 1:3, # limit to selected time points
labels = c("Baseline", "time 1", "time 2"), # label like a discrete scale
limits = c(.75, 3.25)) + # give it some room
scale_shape_identity(guide = "legend",
name = "Treatment",
breaks = c(21, 24), # Defines legend order too; only label the treatment groups
labels = c("A", "B" )) + # this part is tricky - could easily reverse them
scale_fill_identity(guide = "legend",
name = "Treatment",
breaks = c("red", "blue"),# Defines legend order too; only label the treatment groups
labels = c("A", "B" )) + # this part is tricky - could easily reverse them
labs(x=NULL)

reprex package 创建于 2021-09-19 (v2.0.1)

最佳答案

经过一些实验,我找到了一个非常好的方法来做到这一点。似乎您可以使用 position_dodge2(width = c(...)) 来指定单独的闪避宽度。

对于您的示例,指定 width = c(0.000001, 0.2, 0.2):

df_test %>% 
ggplot(aes(timepoint, y, group = group, shape = group, fill = group)) +
geom_linerange(linetype = 1, position = position_dodge2(width = c(0.000001, 0.2, 0.2)), aes(ymin = ymin, ymax = ymax)) +
geom_point(position = position_dodge2(width = c(0.000001, 0.2, 0.2))) +
geom_line(position = position_dodge2(width = c(0.000001, 0.2, 0.2)))

enter image description here

最初,我尝试设置 width = c(0, 0.2, 0.2),但没有按预期运行:

enter image description here

?position_dodge 没有明确解释使用一串值作为闪避宽度,所以我不知道为什么当您使用零作为宽度时它不起作用。 0.000001 的值“足够接近”零,您无法通过查看该图来判断,因此希望这就足够了。

关于r - 如何改变 "position_dodge"一个点而不是 ggplot2 系列中的其他点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69237204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com