2017-01-11 56 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 

我所做的是我使用的參數和XLIM選擇一切,但第一個元素(這是coord_flip之前完成)的coord_flip()函數中。

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

這導致在劇情之外仍然可以看到刻度線。這是通過單獨設置蜱的顏色來解決的。

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