我想要实现的与geom_area类似,但使用geom_bar。条的默认宽度在它们之间留下太多空间。如果这是数据所指示的,我基本上希望条之间没有空格。一个例子是下面的 Plot 2 。但是,这会导致重叠条,现在只适用于Windows。我想实现可变宽度条,其中数据确定宽度。在我的数据的情况下,x轴是日期/时间,我希望条的宽度来自数据收集间隔(dur_m)的持续时间。因此,60分钟的条形将是15分钟条形宽度的4倍。
这是数据的样子:
WAIT_CLASS end SNAP_ID AVG_SESS dur_m barChartWidth
User I/O 2015-03-04 16:59:00 46606 52.24 60 3780
CPU 2015-03-04 16:59:00 46606 26.65 60 3780
User I/O 2015-03-04 17:59:00 46607 48.78 60 3780
CPU 2015-03-04 17:59:00 46607 25.48 60 3780
User I/O 2015-03-04 18:13:00 46608 44.05 14 882
CPU 2015-03-04 18:13:00 46608 29.31 14 882
挑战/注意事项:
可重复的示例代码,包括从github中提取的数据:
# Download 23 kb DF_AAS.Rda which contains one data.table
temporaryFile <- tempfile()
download.file("https://raw.githubusercontent.com/tmuth/public-data/master/R/AAS-Bar-Plot/DF_AAS.Rda",
destfile = temporaryFile, method = "curl")
load(temporaryFile)
library(ggplot2)
library(scales)
library(grid)
plot_aas_chart <- function(DF_AAS_INT,barWidth){
aas_colors <- c("Administrative" = "#6c6e69", "Application" = "#bf2a05", "Cluster" = "#ccc4af", "Commit" = "#e36a05",
"Concurrency" = "#8a1b07","Configuration" = "#5a4611","CPU" = "#05cc04","Network" = "#9b9b7a",
"Other" = "#f06fad","Scheduler" = "#97f797","Queuing" = "#c4b69c",
"System I/O" = "#0993de","User I/O" = "#054ae1")
gg_aas_colors <- scale_fill_manual("", values = aas_colors)
plot_aas_wait_class <- ggplot()+
geom_bar(data=DF_AAS_INT, aes(x = end, y = AVG_SESS,
fill = WAIT_CLASS),stat = "identity", position = "stack",width=barWidth)+
gg_aas_colors+
theme(axis.title.x = element_blank(),axis.title.y = element_blank(),
legend.key.size = unit(0.6, "lines"))+
labs(title=paste("Average Active Sessions (AAS) for DB...","",sep="")
)
return(plot_aas_wait_class)
}
plot_aas_chart(DF_AAS,NULL)
plot_aas_chart(DF_AAS,4000) # barWith=4000
plot_aas_chart(subset(DF_AAS,SNAP_ID < 46614),NULL) # filter down to 1 day
输出: 图1 - NULL宽度 图2 - 宽度= 4000 图3 - 数据过滤到1天,宽度= NULL