第一个问题我已经在堆栈上问过,我对R很新,所以请原谅任何礼仪罪行。我正在使用ggplot2绘制2个堆积面积图。数据是来自Oracle数据库的等待事件。这是一个性能调整图表。我有几个问题。
这是我的代码:
library(ggplot2)
library(RColorBrewer)
library(gridExtra)
DF_AAS <- read.csv('http://dl.dropbox.com/u/4131944/Permanent/R-Questions/AAS-Plot/DATA_FRAME_AAS.csv', head=TRUE,sep=",",stringsAsFactors=TRUE)
DF_AAS <- within(DF_AAS, snap_time <- as.POSIXlt(snap_times2,
format = "%Y-%m-%d %H:%M:%S"))
DF_AAS[c('snap_times2')] <- NULL
DF_AAS_EVENT <- read.csv('http://dl.dropbox.com/u/4131944/Permanent/R-Questions/AAS-Plot/DF_AAS_EVENT.csv', head=TRUE,sep=",",stringsAsFactors=TRUE)
DF_AAS_EVENT <- within(DF_AAS_EVENT, snap_time <- as.POSIXlt(snap_times2,
format = "%Y-%m-%d %H:%M:%S"))
DF_AAS_EVENT[c('snap_times2')] <- NULL
plot_aas_wait_class <- ggplot()+
geom_area(data=DF_AAS, aes(x = snap_time, y = aas,
fill = wait_class),stat = "identity", position = "stack",alpha=.9)+
scale_fill_brewer(palette="Paired",breaks = sort(levels(DF_AAS$wait_class)))+
scale_y_continuous(breaks = seq(0, max(DF_AAS$aas)+(max(DF_AAS$aas)*.2), 5))+
opts(panel.background = theme_rect(colour = "#aaaaaa"))
plot_aas_event <- ggplot()+
geom_area(data=DF_AAS_EVENT, aes(x = snap_time, y = aas,
fill = wait_class_event),stat = "identity", position = "stack")+
scale_fill_brewer(palette="Paired",breaks = DF_AAS_EVENT$wait_class_event)+
scale_y_continuous(breaks = seq(0, max(DF_AAS_EVENT$aas)+(max(DF_AAS_EVENT$aas)*.2), 5))+
opts( panel.background = theme_rect(colour = "#aaaaaa"))
grid.arrange(arrangeGrob(plot_aas_wait_class, plot_aas_event),heights=c(1/2,1/2),ncol=1)
答案 0 :(得分:1)
对齐问题的最简单解决方案可能是移动传说:
library(scales)
plot_aas_wait_class <- ggplot()+
geom_area(data=DF_AAS, aes(x = snap_time, y = aas,fill = wait_class),stat = "identity", position = "stack",alpha=.9)+
scale_fill_brewer(palette="Paired",breaks = sort(levels(DF_AAS$wait_class)))+
scale_y_continuous(breaks = seq(0, max(DF_AAS$aas)+(max(DF_AAS$aas)*.2), 5))+
opts(panel.background = theme_rect(colour = "#aaaaaa")) +
opts(legend.position = "bottom",legend.direction = "horizontal") +
guides(fill = guide_legend(nrow = 2))
plot_aas_event <- ggplot()+
geom_area(data=DF_AAS_EVENT, aes(x = snap_time, y = aas,fill = wait_class_event),stat = "identity", position = "stack")+
scale_fill_brewer(palette="Paired",breaks = DF_AAS_EVENT$wait_class_event)+
scale_y_continuous(breaks = seq(0, max(DF_AAS_EVENT$aas)+(max(DF_AAS_EVENT$aas)*.2), 5))+
opts( panel.background = theme_rect(colour = "#aaaaaa")) +
opts(legend.position = "bottom",legend.direction = "horizontal") +
guides(fill = guide_legend(ncol = 2))
grid.arrange(arrangeGrob(plot_aas_wait_class, plot_aas_event),heights=c(1/2,1/2),ncol=1)
要提高x轴的分辨率,我会使用类似的东西:
+ scale_x_datetime(breaks = date_breaks("2 hours"))
或者你喜欢的任何休息。
通常使用geom_rect
并设置alpha = 0.25
或其他内容来着色特定区域。这需要创建一个单独的数据框,其中包含rect的起点和终点(使用Inf
和-Inf
作为y坐标)以传递给geom_rect
。