如何在rstudio中绘制堆叠和分组的棒块图?

发布于 2025-01-20 22:48:48 字数 1168 浏览 1 评论 0原文

我已经创建了一个分组的Barchart,该Barchart显示了按年龄组和性别分组的客户端数(n = 25)。但是现在,我需要包括所有筛选研究以显示数据质量的客户(n = 52)。它应该看起来像附着的Barchart,但总数以较浅的颜色堆叠,因此可以看到:在年龄组1中,我们筛选了15位女性和9名男性,但最终只有8位女性注册了学习。我希望这样可以理解。我将添加现有的条形图。

该图表的代码如下:

ggplot(data=daten, aes(x=AG, y=stat(count), group=factor(GESCHLECHT), fill=factor(GESCHLECHT)))+
        geom_bar(position=position_dodge2(preserve="single", padding=0))+
  scale_fill_manual(values = c("steelblue","darkred"), labels=c("Männlich", "Weiblich"))+
  labs(y="Anzahl", x="Altersgruppen", fill="Geschlecht:")+
  scale_x_continuous(breaks=seq(1,3, by=1), labels=c("25 bis <51","51 bis <65", ">65"))+
  scale_y_continuous(limits=c(0, 10))+
  geom_text(aes(label=stat(count)),stat="count", vjust=-0.5, position=position_dodge(width = 1))+
  theme(legend.position="bottom")

我不知道如何组织我的数据,以便我可以做这样的事情。最后,应该看起来像第一个栏上方(n = 8),只有较浅的颜色才能继续,然后用n = 6个女性说(最终表明我们筛选了15在该年龄组中总共是女性)

我认为首先,我需要使用所有n = 52个客户端创建一个数据框架,并具有针对年龄组和性别的代码。但是,如何将杠铃分开,以使筛选和招募客户之间的区别变得清晰?

I already created a grouped barchart, that shows the number of clients (n=25) grouped by agegroup and gender. But now I need to include all clients (n=52) that were screened for the study to show quality of data. It should look like the attached barchart, but with the total number stacked in a lighter color, so one can see: In Age Group 1, we screened for example 15 females and 9 males, but in the end only 8 females signed up for the study. I hope it is understandable like this. I'll add the existing bar chart.

the code for this chart is the following:
enter image description here

ggplot(data=daten, aes(x=AG, y=stat(count), group=factor(GESCHLECHT), fill=factor(GESCHLECHT)))+
        geom_bar(position=position_dodge2(preserve="single", padding=0))+
  scale_fill_manual(values = c("steelblue","darkred"), labels=c("Männlich", "Weiblich"))+
  labs(y="Anzahl", x="Altersgruppen", fill="Geschlecht:")+
  scale_x_continuous(breaks=seq(1,3, by=1), labels=c("25 bis <51","51 bis <65", ">65"))+
  scale_y_continuous(limits=c(0, 10))+
  geom_text(aes(label=stat(count)),stat="count", vjust=-0.5, position=position_dodge(width = 1))+
  theme(legend.position="bottom")

I have no idea how to organize my data so that I could do something like that. In the end it should look like that above the first bar (n=8) the bar should continue, only with for example a lighter color and then lets say with n=6 more females (in the end that would show that we screened 15 females in total in that agegroup)

I think first I would need to create a data frame with all n=52 clients, with a code for Age Group and gender. But how can I divide the bars so that the difference between screened and recruited clients become clear?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2025-01-27 22:48:48

像这样的东西吗?

library(tidyverse)

set.seed(1)
# example data
n <- 100
data <- tibble(
  gender = sample(c("m", "f"), n, replace = TRUE),
  age = runif(n, 25, 70) %>% as.integer(),
  screened = rep(TRUE, 0.8 * n) %>% c(rep(FALSE, 0.2 * n)) %>% sample()
)
data
#> # A tibble: 100 × 3
#>    gender   age screened
#>    <chr>  <int> <lgl>   
#>  1 m         54 TRUE    
#>  2 f         40 FALSE   
#>  3 m         37 FALSE   
#>  4 m         69 TRUE    
#>  5 f         53 TRUE    
#>  6 m         34 TRUE    
#>  7 m         30 TRUE    
#>  8 m         46 TRUE    
#>  9 f         66 TRUE    
#> 10 f         51 TRUE    
#> # … with 90 more rows

data %>%
  mutate(
    age_group = age %>% cut(breaks = c(0, 25, 51, 65, Inf))
  ) %>%
  ggplot(aes(x = age_group, fill = gender, alpha = screened)) +
  geom_bar() +
  scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = 0.5))

reprex 包 (v2.0.0)

Something like this?

library(tidyverse)

set.seed(1)
# example data
n <- 100
data <- tibble(
  gender = sample(c("m", "f"), n, replace = TRUE),
  age = runif(n, 25, 70) %>% as.integer(),
  screened = rep(TRUE, 0.8 * n) %>% c(rep(FALSE, 0.2 * n)) %>% sample()
)
data
#> # A tibble: 100 × 3
#>    gender   age screened
#>    <chr>  <int> <lgl>   
#>  1 m         54 TRUE    
#>  2 f         40 FALSE   
#>  3 m         37 FALSE   
#>  4 m         69 TRUE    
#>  5 f         53 TRUE    
#>  6 m         34 TRUE    
#>  7 m         30 TRUE    
#>  8 m         46 TRUE    
#>  9 f         66 TRUE    
#> 10 f         51 TRUE    
#> # … with 90 more rows

data %>%
  mutate(
    age_group = age %>% cut(breaks = c(0, 25, 51, 65, Inf))
  ) %>%
  ggplot(aes(x = age_group, fill = gender, alpha = screened)) +
  geom_bar() +
  scale_alpha_manual(values = c(`TRUE` = 1, `FALSE` = 0.5))

Created on 2022-04-13 by the reprex package (v2.0.0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文