I’m trying to design a bar graph that represents percentages of complications among two cohorts (GLP and No GLP). I am using the data table and code below, and I have included my current result as well as a depiction of the graph I’m trying to obtain.
Any help would be appreciated. Data table below saved as GLPcomprate.
Cohort | Complication | Percentage |
---|---|---|
GLP | CSF_leak | 13.40 |
GLP | DVT | 5.74 |
GLP | UTI | 20.57 |
GLP | Constipation | 22.97 |
GLP | Pneumonia | 9.09 |
GLP | Post-operative Infection | 6.22 |
GLP | Adverse Drug Effects | 4.79 |
GLP | Meningitis | 4.79 |
GLP | Post-operative Respiratory Failure | 4.79 |
GLP | Thyrotoxicosis | 4.79 |
GLP | Acute Kidney Injury | 11.96 |
GLP | Delirium | 4.79 |
No GLP | CSF_leak | 11.32 |
No GLP | DVT | 5.26 |
No GLP | UTI | 15.74 |
No GLP | Constipation | 24.73 |
No GLP | Pneumonia | 9.81 |
No GLP | Post-operative Infection | 5.26 |
No GLP | Adverse Drug Effects | 0.65 |
No GLP | Meningitis | 7.12 |
No GLP | Post-operative Respiratory Failure | 1.71 |
No GLP | Thyrotoxicosis | 1.00 |
No GLP | Acute Kidney Injury | 10.78 |
No GLP | Delirium | 2.46 |
Current code is below:
GLPcomprate <- read_excel("IIH_GLP_Years.xlsx", sheet = 6)
head(GLPcomprate)
ggplot(GLPcomprate, aes(x=Complication, y=Percentage, fill = Cohort)) +
geom_bar(stat = "identity", position = position_stack(), width = 1) +
geom_text(aes(label = paste(Percentage, "%")), vjust = -1) +
facet_wrap(~Cohort, strip.position = "bottom", scales = "free_x") +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside ")
Current output:
Desired output (created on Excel):
Just use position_dodge()
. The result from the ggplot2
defaults made the plot cluttered so I have added some adjustments, tweak where necessary. Also, there was a typo in your example data, remember R is case-sensitive.
library(ggplot2)
# Data
GLPcomprate <- read.table(text = "Cohort,Complication,Percentage
GLP,CSF_leak,13.40
GLP,DVT,5.74
GLP,UTI,20.57
GLP,Constipation,22.97
GLP,Pneumonia,9.09
GLP,Post-operative Infection,6.22
GLP,Adverse Drug Effects,4.79
GLP,Meningitis,4.79
GLP,Post-operative Respiratory Failure,4.79
GLP,Thyrotoxicosis,4.79
GLP,Acute Kidney Injury,11.96
GLP,Delirium,4.79
No GLP,CSF_leak,11.32
No GLP,DVT,5.26
No GLP,UTI,15.74
No GLP,Constipation,24.73
No GLP,Pneumonia,9.81
No GLP,Post-operative Infection,5.26
No GLP,Adverse Drug Effects,0.65
No GLP,Meningitis,7.12
No GLP,Post-operative Respiratory Failure,1.71
No GLP,Thyrotoxicosis,1.00
No GLP,Acute Kidney Injury,10.78
No GLP,Delirium,2.46", header = TRUE, sep = ",")
ggplot(GLPcomprate,
aes(x = Complication, y = Percentage, fill = Cohort)) +
geom_bar(stat = "identity", position = position_dodge(), width = 1) +
geom_text(aes(label = paste0(Percentage, "%")),
size = 2,
hjust = 0.5,
position = position_dodge(width = .9)) +
coord_cartesian(clip = "off") +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_blank(),
strip.placement = "outside ",
axis.text.x = element_text(angle = 45, hjust = 1, size = 6))
ggplot(GLPcomprate, aes(fct_inorder(Complication), y=Percentage, fill = Cohort)) +
geom_col(position = position_dodge(width = 0.6), width = 0.4) +
scale_y_continuous(labels = scales::label_number(accuracy = 0.01), limits = c(0, 30), breaks = seq(0, 30, 5)) +
ggthemes::scale_fill_excel_new(labels = c('GLP Percentage', 'No GLP Percentage')) +
ggthemes::theme_excel_new() +
theme(
panel.grid.major.x = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1)
) +
labs(title = 'Complication Rate Among Cohorts')