Hi would like to compare two categories to whole categories using facet_grid or facet_wrap or another function in ggplot. For example i would like to compare statistics of Hospitals 3 and 4 to the whole hospitals.
Hospital<-c("Hosp1","Hosp1","Hosp1","Hosp1","Hosp1",
"Hosp2","Hosp2","Hosp2","Hosp2","Hosp2",
"Hosp3","Hosp3","Hosp3","Hosp3","Hosp3",
"Hosp4","Hosp4","Hosp4","Hosp4","Hosp4")
Disease<-c("D1","D1","D2","D2","D3",
"D1","D1","D1","D3","D3",
"D3","D3","D2","D2","D3",
"D1","D1","D2","D2","D2")
data<-data.frame(Hospital,Disease)
plot<-ggplot(data, aes(x=Disease,fill=Disease))+
geom_bar()+facet_grid(~Hospital)+coord_flip()
Using facet_grid, I have a graph that compares the four hospitals, which I do not want.
I rather want something like this with facets without going through "grid.arrange", because I want to display all disease categories (even if they are null) for all graphs (in order to easily compare) and I don't want the x.axis label to be displayed for each graph because it takes a lot of space
wh<-ggplot(data, aes(x=Disease,fill=Disease))+
geom_bar()+coord_flip()+labs(title = "whole hospital")
H3<-ggplot(data[data$Hospital=="Hosp3",], aes(x=Disease,
fill=Disease))+ geom_bar()+coord_flip()+
labs(title = "hospital3")
H4<-ggplot(data[data$Hospital=="Hosp4",], aes(x=Disease,
fill=Disease))+ geom_bar()+coord_flip()+
labs(title = "hospital4")
grid.arrange(wh,H3,H4,ncol=3)
How about this based on gghighlight
library(ggplot2)
library(dplyr)
data_all <-
data %>%
mutate(Hospital = "Hosp_all") %>%
group_by(Disease) %>%
summarise(total = n())
data %>%
filter(Hospital %in% c("Hosp3", "Hosp4")) %>%
ggplot(aes(x = Disease, fill = Disease))+
geom_col(data = data_all, aes(Disease, total), fill = "gray80")+
geom_bar()+
coord_flip()+
facet_wrap(~Hospital)+
theme(legend.position = "bottom")
Created on 2020-06-23 by the reprex package (v0.3.0)