2017-01-11 3 views
1

병원 병동에있는 특정/표시된 생물체의 수를 보여주는 월간 그래프 용 그래프 생성을 자동화/반 자동화하려고합니다. 이 작업을 위해 누적 막 대형 차트를 사용하고 있습니다. 문제는 내 데이터 집합에서 유기체를 제외하면 내 범례에 분명히 표시되지 않는다는 것입니다. 내 바보 같은 해결 방법은 특정 달에 발견되지 않은 병균을 추가하고 와드를 아무 것도 아니거나 오히려 ""비어있는 상태로 진술하는 것입니다. 이렇게하면 내가 원하는 방식으로 전설을 알 수 있습니다. 이제 문제는 차트의 맨 아래에서 볼 수 있듯이 나와 있습니다. 이것은 전문가가 아닌 것처럼 보입니다. 내가 시도한 다른 것은 geom_blank를 사용하고 내가 원하는 방식으로 레이블을 추가하는 것이지만 테스트되지 않은 유기체는 포함하지 않는다. 작동하지 않는다. 전설이 포함 된 데이터와 상관없이 내가 원한대로되게하는 방법이 있습니까?R ggplot geom_bar, 데이터가 누락 된 경우에도 라벨을 포함하십시오.

library(ggplot2) 

ward_stats <- read.csv("ward_stats.csv") 

#specific colors for specific organisms 
my_colours <- c("Acinetobacter baumannii (Carbapenem resisistant)" = "red3", 
       "Pseudomonas aeruginosa (MDR)" = "gold", 
       "Enterobacter cloacae (ESBL)" = "purple", 
       "Enterococcus faecium (VRE)" = "violet", 
       "Escherichia coli (ESBL)" = "dodgerblue1", 
       "Klebsiella pneumoniae (ESBL)" = "yellowgreen", 
       "Mycobacterium tuberculosis complex" = "black", 
       "Staphylococcus aureus (MRSA)" = "turquoise", 
       "Klebsiella pneumoniae (Carbapenem resistant)" = "grey", 
       "Clostridium difficile" = "sienna4") 

#A vector of organisms on the flag list in the order we want to show in the legend 
targetOrder <- c("Acinetobacter baumannii (Carbapenem resisistant)", "Pseudomonas aeruginosa (MDR)", 
       "Enterobacter cloacae (ESBL)", "Escherichia coli (ESBL)", "Klebsiella pneumoniae (ESBL)", 
       "Klebsiella pneumoniae (Carbapenem resistant)", "Staphylococcus aureus (MRSA)", "Enterococcus faecium (VRE)", 
       "Clostridium difficile", "Mycobacterium tuberculosis complex") 


p <- ggplot(data=ward_stats,aes(x=ward_stats$Ward.Name, 
           y=ward_stats$freq, 
           fill=ward_stats$Result...Organism.Identified)) 
p <- p + geom_bar(stat="identity") 
p <- p + geom_text(aes(y=cum_freq, label=freq), hjust= 2, color='white') 
p <- p + coord_flip() 
p <- p + ggtitle("Hospital") 
p <- p + theme(plot.title = element_text(size=20, face="bold")) 
p <- p + labs(x=NULL, y= "Number cultured per ward", vjust = -2) 
p <- p + theme(axis.title.x = element_text(color="black", vjust=-2, size=12)) 
p <- p + theme(axis.text.x=element_text(size=10, vjust=0.5)) 
p <- p + theme(legend.title=element_blank()) 
p <- p + scale_fill_manual(values = my_colours, breaks = targetOrder) 
p <- p + theme(panel.background = element_rect(fill="#e6e6ff")) 

#pdf_title <- paste(graph_title,".pdf", sep="") 
#ggsave("graph.pdf", plot=p, width = 10, height = 8, units = "in") 

print(p) 
위의 링크에서 그래프의 하단에

my_graph

이봐, 틱에서 빈 빈 곳이 /가 있습니다. 내가 무슨 짓을

The Data

+0

내가 틀렸을 수도 있지만 플로팅하기 전에 레벨을 떨어 뜨리려고 한 적이 있습니까? –

답변

0

꽤 광고 특별이지만 작동합니다. 내가 무슨 짓을

p <- ggplot(data=ward_stats,aes(x=ward_stats$Ward.Name, 
           y=ward_stats$freq, 
           fill=ward_stats$Result...Organism.Identified)) 
p <- p + geom_bar(stat="identity") 
p <- p + geom_text(aes(y=cum_freq, label=freq), hjust= 2, color='white') 
p <- p + coord_flip(xlim = c(2, 23)) 
p <- p + ggtitle("Hospital") 
p <- p + theme(plot.title = element_text(size=20, face="bold")) 
p <- p + labs(x=NULL, y= "Number cultured per ward", vjust = -2) 
p <- p + theme(axis.title.x = element_text(color="black", vjust=-2, size=12)) 
p <- p + theme(axis.text.x=element_text(size=10, vjust=0.5)) 
p <- p + theme(legend.title=element_blank()) 
p <- p + scale_fill_manual(values = my_colours, breaks = targetOrder) 
p <- p + theme(panel.background = element_rect(fill="#e6e6ff")) 
p <- p + theme(axis.ticks.y = element_line(colour = c('white', rep('black', 22)))) 

p 

내가 (이것은 coord_flip 전에 완료) 인수 xlim을 사용하고, 첫 번째 요소를 제외한 모든를 선택한 coord_flip() 함수 안에 있습니다.

p <- p + coord_flip(xlim = c(2, 23)) 

이로 인해 눈금이 여전히 플롯 외부에서 가시화 될 수있었습니다. 이것은 틱에 대해 개별적으로 색을 설정하여 수정되었습니다.

p <- p + theme(axis.ticks.y = element_line(colour = c('white', rep('black', 22))))