我正在尝试自动生成/半自动生成图表,用于显示医院病房中特定/标记生物数量的月度图表。我正在使用堆积条形图来执行此操作。问题是,如果我从我的数据集中排除有机体,它显然不会在我的图例中显示。我愚蠢的解决方法是添加在特定月份找不到的有机体,并将病房说成没有或者更确切地说#34;"或空的。这样我就可以按照我想要的方式获得传奇。现在的问题是,正如您在图表底部看到的那样,我显示了"" - 区域。这看起来不专业。我尝试过的其他方法是使用geom_blank并按照我想要的方式添加标签,但不包括未经测试的生物 - 它不起作用。有没有办法强制传说完全符合我的要求,无论是否包含数据。
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)
从上面的链接看图表的底部,注意在勾号处有一个空位/空白。
答案 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))))