R ggplot2问题与镜像密度图混合的3个变量的堆叠条形图的问题。

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

亲爱的Stackoverflow社区。

再一次,我有一个关于R的ggplot2可能性的问题,在我开始解释我的问题之前,这里提供了一个数据框的例子。

age <- c(12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15)
anticoagulation <- c(0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1)
atc <- c(1, 0, 2, 0, 1, 2, 1, 0, 2, 0, 1, 2, 1, 0, 2, 0, 1, 2, 0, 0)
df <- data.frame(age, anticoagulation, atc)
  • 抗凝血编码:0=无抗凝血,1=收到 0=没有抗凝,1=接受了抗凝。
  • atc编码。0 = 硝基呋喃妥因,1 = 磷霉素,2 = 曲安奈德。

我想把每个年龄组和每个Atc组的抗凝处方差异可视化。到目前为止,我所做的一切。

frame <- aggregate(df$anticoagulation, by=list(df$age), FUN=length)
frame$age <- frame$Group.1
frame$n <- frame$x
frame <- frame [,3:4]

my_table<- table(df$age, df$anticoagulation)
table <- as.data.frame.matrix(my_table)
frame$n_noanti <- table$"0"
frame$n_yesanti <- table$"1"

frame$per_yesanti <- (frame$n_yesanti/frame$n)*100 # percentage
frame$per_noanti <- (frame$n_noanti/frame$n)*100 # percentage


ggplot(frame, aes(x=x) ) +
  geom_bar( aes(x = reorder (age, -per_yesanti), y =per_yesanti), stat="identity", fill="#69b3a2" ) +
  geom_label(aes(x=15, y=100, label="Used anticoagulants"), color="#69b3a2")+
  geom_bar( aes( x =reorder (age, -per_noanti), y=-per_noanti), stat="identity", fill="#404080" ) +
  geom_label( aes(x=15, y=-100, label="No anticoagulants"), color="#404080") +
  theme(axis.text.x=element_blank()) + 
  xlab ("Age") + 
  ylab ("Percentages of how many women used anticoagulants")+
  ggtitle("Distribution of anticoagulants per age")+
  theme(plot.title = element_text(hjust = 0.5), text = element_text(size=15))

输出以上是ggplot镜像密度的输出结果

然而,我想有这样一个图形,但像这样的堆积条。叠加条形图的例子

叠加的部分是基于atc编码的。我曾试过只做一个堆叠图,但失败得很惨。

我曾试过用'聚合'的代码,但我纠结于该用什么,该合并在一起。

frame2 <- aggregate(frame$anticoagulation, by=list(frame$age, frame$atc), FUN=length)

但是,这个聚合代码让它变得太长,无法使用。

我也试过,就是用单独的聚合代码来处理atc与年龄,然后把它加到'框架'中。

atc2<- table(df$age, df$atc)
t_atc2 <- as.data.frame.matrix(atc2)
frame$n_nitro <- t_atc2$"0"
frame$n_fosfo <- t_atc2$"1"
frame$n_trim <- t_atc2$"2"

但是,我还是无法让堆积函数工作。我的尝试是用抗凝百分比=是(编码=1)=做一个堆积条。

    ggplot(frame, aes(fill = n_nitro+n_fosfo+n_trim, y=per_yesanti, x=age)) + 
  geom_bar(position="stack", stat="identity") +
  ggtitle("Anticoagulation per age")

图。两组ATC之间没有区别

我希望有人能把这两张图混在一起。如果这是很不可能的比只有抗凝百分比=1(per_yesanti)的叠加图也不错。

所以,总之,如果混合图是非常困难的。我怎么能做出下面的图(所以只有1个图)。

  • 只有细节与抗凝剂=1是的
  • 抗凝剂的详细情况必须以百分比计算(按总抗凝剂是与否计算)
  • x轴为每个年龄段
  • DE条必须由ATC来填写

像这样。在这里输入图像描述

先谢谢你

r ggplot2 stacked-chart stackedbarseries
1个回答
0
投票

我还是不知道该如何看待你的数据,但我试着给出一个答案。要想直接用另一个变量分组得到基于百分比的条形图有点困难,在 ggplot2. 因此,最简单的解决办法是事先计算好百分比,然后用 geom_col 来绘制这些。

使用 dplyr,你可以 group_by 两者 age 和其他你想要叠加分离的变量。

age <- c(12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15, 12, 13, 14, 15)
anticoagulation <- c(0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1)
atc <- c(1, 0, 2, 0, 1, 2, 1, 0, 2, 0, 1, 2, 1, 0, 2, 0, 1, 2, 0, 0)
df <- data.frame(age, anticoagulation, atc)

library(dplyr)
library(ggplot2)

df_summary <- df %>% 
  group_by(age, anticoagulation) %>% 
  summarise(count = n()) %>% 
  mutate(percentage = count / sum(count) * 100)


ggplot(df_summary, aes(x = factor(age), y = percentage, fill = factor(anticoagulation))) +
         geom_col()

enter image description here

df_summary_2 <- df %>% 
  group_by(age, atc) %>% 
  summarise(count = n()) %>% 
  mutate(percentage = count / sum(count) * 100)

ggplot(df_summary_2, aes(x = factor(age), y = percentage, fill = factor(atc))) +
  geom_col()

enter image description here


编辑

我已经改编了我的图。我无法想出一个一次性计算所有东西的方案。所以我先计算每个年龄段的人数,以 total_count_info. 这样我就可以在以后计算出每个年龄组的百分比。然后,我计算了以下情况的发生率 atcageanticoagulation:

total_count_info <- df %>% 
  group_by(age) %>% 
  summarise(count_age = n())

df_summary_3 <- df %>% 
  group_by(age, anticoagulation, atc) %>% 
  summarise(count = n()) %>% 
  left_join(total_count_info) %>% 
  mutate(percentage = count / count_age * 100)


ggplot(df_summary_3 %>% filter(anticoagulation == 1),
aes(x = factor(age), y = percentage, fill = factor(atc))) +
  geom_col() +
  ylab("percentage of anticoagulation == 1")

enter image description here

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