jeudi 30 avril 2020

Conditionnaly color barplots

I would like to conditionally color these mirrors barplots. I want to colour according to the disease and the hospital. I want the diseases that have the first two identical characters to have the same colour (i.e. D4001 and D4002 will have the same colour; D3000, D3001 and D3003 will have the same colour). I also want to stratify the color on the hospital so that the transparency or contrast of the barplot will be different from one hospital to another for the same disease (for example a disease D4 in hospital A will be red and a disease D4 in hospital B will be transparent red).

set.seed(0)
ID=1:20
Hospital<-sample(c(rep("A",10),rep("B",10)))
Disease<-c("D1000",rep("D2001",2),rep("D2000",3),rep("D3000",4),
rep("D3001",2),rep("D3003",4),rep("D4001",3),"D4002")

data$Disease<-as.factor(data$Disease)
data<-data.frame(ID,Hospital,Disease)
datacount<-data%>%group_by(Hospital,Disease)%>%count
datacount$n2<-ifelse(datacount$Hospital=="B",datacount$n,-datacount$n)

ggplot(datacount,aes(x=Disease,y=n2))+
  geom_col(aes(fill=Hospital))+coord_flip()

All I know how to do is either color by hospital or by disease individually.

enter image description here enter image description here

Aucun commentaire:

Enregistrer un commentaire