【问题标题】:create geom_segment() per factor每个因素创建 geom_segment()
【发布时间】:2023-01-19 21:32:38
【问题描述】:

我想在每个 x 因子的 y 平均值处绘制水平线段。

预期输出是这样的:

我的方法是使用geom_segment(),但我收到关于 aes 长度的错误。

美学必须是长度 1 或与数据 (2) 相同:y

library(tidyverse)
library(ggnewscale)      

# data
df <- structure(list(d = c(1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 
0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L), y = c(6, 5, 4, 4, 5, 
3, 4, 6, 6, 6, 4, 6, 5, 5, 5, 4, 4, 4, 6, 6), z = c(1L, 0L, 1L, 
1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-20L))

# group means
means <- df %>% 
  group_by(d) %>% 
  summarize(y = mean(y)) %>%
  mutate(d = factor(d)) %>%
  mutate(x = case_when(
    d==0 ~ 0.5,
    TRUE ~ 1.5
  )) %>%
  mutate(xend = case_when(
    d==0 ~ 1.5,
    TRUE ~ 2.5 
  )) %>%
  mutate(yend = y)

# plot
df %>%
  mutate(z = factor(z),
         d = factor(d)
  ) %>%
  ggplot(aes(x=d, 
             y=y, 
             color=z)) +
  geom_point(position = position_jitter(seed = 42),
             alpha = 0.7) + 
  scale_color_manual(values = c("black", "#CC0200")) +
  new_scale_colour() +
  geom_segment(data = means, 
               aes(x = x, y = y, xend = xend, yend = yend, 
                   colour = d)) +
  scale_color_manual(values = c("#e69138", "#1f9ac9")) +
  guides(color = "none")

【问题讨论】:

  • 打字错误?我想知道 y = mean 是否应该改为 y = y(因为你的 means 数据集的名称为 c("d", "y", "x", "xend", "yend"),但都不是 "mean")。这会为我返回一个错误(因为 mean 这里是功能),也许你有一个来自过去计算的名为 mean 的对象?)解决这个问题,我看到有两条线的分解散点图。
  • 谢谢,@r2evans 这绝对是一个错字。固定在上面。从用例转移到玩具示例的错误。修复它虽然给我一个关于连续值与离散值的错误。我正在排除故障......
  • 如果我关闭new_scale_colour(),我会得到点和线段,但这给了我错误的颜色。出于一个复杂的原因,我使用 new_scale_colour() 为点和线段设置不同的颜色。
  • 无法重现您的问题,但出现了不同的错误 object 'z' not found。通过将 color=z 移至 geom_point() 解决此问题后,您的代码工作正常并给出了预期的结果。
  • 就是这样!谢谢

标签: r ggplot2


【解决方案1】:

@stefan 的解决方案有效:将color=z 移至geom_point()

library(tidyverse)
library(ggnewscale)      

# data
df <- structure(list(d = c(1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 
0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L), y = c(6, 5, 4, 4, 5, 
3, 4, 6, 6, 6, 4, 6, 5, 5, 5, 4, 4, 4, 6, 6), z = c(1L, 0L, 1L, 
1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-20L))

# group means
means <- df %>% 
  group_by(d) %>% 
  summarize(y = mean(y)) %>%
  mutate(d = factor(d)) %>%
  mutate(x = case_when(
    d==0 ~ 0.5,
    TRUE ~ 1.5
  )) %>%
  mutate(xend = case_when(
    d==0 ~ 1.5,
    TRUE ~ 2.5 
  )) %>%
  mutate(yend = y)

# plot
df %>%
  mutate(z = factor(z),
         d = factor(d)
  ) %>%
  ggplot(aes(x=d, 
             y=y)) +
  geom_point(position = position_jitter(seed = 42),
             alpha = 0.7, aes(color=z)) + 
  scale_color_manual(values = c("black", "#CC0200")) +
  new_scale_colour() +
  geom_segment(data = means, 
               aes(x = x, y = y, xend = xend, yend = yend, 
                   colour = d)) +
  scale_color_manual(values = c("#e69138", "#1f9ac9")) +
  guides(color = "none")

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多