【问题标题】:Bar chart + geom_jitter with the points of the jitter inside the fills of the bar chart条形图 + geom_jitter 与条形图填充内的抖动点
【发布时间】:2021-12-10 17:54:27
【问题描述】:

我想要一个条形图,其中包含我正在研究的人口分布,这个条形图的 x 轴为年龄(计数为 y),填充为种族。我想用 geom_scatter 覆盖相应组内的一些主题。但是,它创建了自己的轴。 我无法分享数据,但这是一个虚拟小标题

df = tribble(
  ~id, ~agegroup, ~ethnicity,
  #--|--|----
  "a", "20s", "African Descent",
  "b", "30s", "White",
  "c", "50s", "White",
  "d", "40s", "Hispanic",
  "e", "20s", "White",
  "f", "30s", "Hispanic",
  "g", "20s", "Hispanic",
  "h", "30s", "White",
  "i", "20s", "African Descent",
  "j", "30s", "White",
  "k", "50s", "White",
  "l", "20s", "White",
  "m", "30s", "Hispanic",
  "n", "20s", "Hispanic",
  "o", "30s", "White",
  
)
df
dmplot <- ggplot(df, aes(x = agegroup, fill = ethnicity )) +
  geom_bar(stat = "count")+
  labs(
    x = "Age Group",
    y = paste0("Population (total = ", df %>% nrow(), ")"))+
  geom_jitter(df,
              aes(x = agegroup,
                  y = ethnicity)) # this is where I would need to retrieve the geom_bar fill location
dmplot

【问题讨论】:

  • 请分享示例数据和您现有的代码。
  • 您好,感谢您的帮助,我添加了 tibble 和我的 ggplot 代码

标签: r ggplot2 data-visualization


【解决方案1】:

这有点hacky,但我认为这样的事情会做到这一点。不幸的是,您似乎无法将抖动高度分配给美学,但您也许可以找到另一种方法使高度取决于矩形的高度。

df = tribble(
  ~id, ~agegroup, ~ethnicity,
  #--|--|----
  "a", "20s", "African Descent",
  "b", "30s", "White",
  "c", "50s", "White",
  "d", "40s", "Hispanic",
  "e", "20s", "White",
  "f", "30s", "Hispanic",
  "g", "20s", "Hispanic",
  "h", "30s", "White",
  "i", "20s", "African Descent",
  "j", "30s", "White",
  "k", "50s", "White",
  "l", "20s", "White",
  "m", "30s", "Hispanic",
  "n", "20s", "Hispanic",
  "o", "30s", "White",
  
)
df_2 <- df %>%
  count(agegroup, ethnicity) %>%
  group_by(agegroup ) %>%
  mutate(top_rect = cumsum(n),
         bottom_rect = lag(top_rect, default = 0))

df_2_uncounted <- df_2 %>%
  ungroup() %>%
  uncount(n)


ggplot(df_2) +
  geom_rect( aes(xmin = as.numeric(as.factor(agegroup)) - .45,
                 xmax=  as.numeric(as.factor(agegroup)) + .45,
                 ymin = bottom_rect, 
                 ymax = top_rect,
                 fill = ethnicity )) +
  geom_jitter(data = df_2_uncounted,
              aes(x = as.numeric(as.factor(agegroup)),
                  y = (bottom_rect + top_rect)/2),
              width = .3,
              height = .5) +
  scale_x_continuous(breaks = unique(as.numeric(as.factor(df_2$agegroup))), 
                     labels = levels(as.factor(df_2$agegroup))) + 
  labs(
    x = "Age Group",
    y = paste0("Population (total = ", df_2_uncounted %>% nrow(), ")"))

更新

现在有标签


df_2_uncounted <- df_2 %>%
  ungroup() %>%
  uncount(n)%>%
  arrange(agegroup, ethnicity) %>%
  group_by(agegroup, ethnicity) %>%
  mutate(id2 = 1:n()) %>%
  left_join(df %>%
              arrange(agegroup, ethnicity) %>%
              group_by(agegroup, ethnicity) %>%
              mutate(id2 = 1:n()),
            by = c("agegroup", "ethnicity", "id2"))


ggplot(df_2) +
  geom_rect( aes(xmin = as.numeric(as.factor(agegroup)) - .45,
                 xmax=  as.numeric(as.factor(agegroup)) + .45,
                 ymin = bottom_rect, 
                 ymax = top_rect,
                 fill = ethnicity )) +
  geom_jitter(data = df_2_uncounted,
              aes(x = as.numeric(as.factor(agegroup)),
                  y = (bottom_rect + top_rect)/2),
              position = position_jitter(seed = 1, height =0.5)) +
  geom_text(data = df_2_uncounted,
            aes(x = as.numeric(as.factor(agegroup)),
                y = (bottom_rect + top_rect)/2,
                label = id),
            position = position_jitter(seed = 1, height =0.5))+
  scale_x_continuous(breaks = unique(as.numeric(as.factor(df_2$agegroup))), 
                     labels = levels(as.factor(df_2$agegroup))) + 
  labs(
    x = "Age Group",
    y = paste0("Population (total = ", df_2_uncounted %>% nrow(), ")"))

【讨论】:

  • 非常感谢您的时间和精力,它工作得很好,但我想用箱线图做类似的事情,如果有一个更简单的解决方案会更好。也许我应该直接寻求涉及 d3.js 的解决方案,但这是相当的努力(d3 很难大声笑)。在此期间,我会使用这个非常感谢!
  • 对于boxlplots,这其实很容易实现:r-graph-gallery.com/89-box-and-scatter-plot-with-ggplot2.html
  • 嗨,后续问题,我想保留 ID,我可以使用您的流程吗?
  • 是的。请参阅更新:)。再次肯定有点hacky,但它有效
  • 非常感谢它的完美,我设法使其适应我的数据结构,这很棒。再次感谢 !祝您度过愉快的一周!
猜你喜欢
  • 1970-01-01
  • 2016-01-15
  • 2015-07-13
  • 1970-01-01
  • 2020-08-20
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
相关资源
最近更新 更多