R - ggplot2,几个问题,多个相关的情节 [英] R - ggplot2, several questions, multiple correlated plots
问题描述
第一个问题我已经问过,我对R很新,所以请原谅任何礼仪罪行。我正在使用ggplot2绘制2个堆积面积图。数据是来自Oracle数据库的等待事件。这是一个性能调整图表。我有几个问题。
First question I've asked on stack and I'm pretty new to R, so please pardon any etiquette offenses. I'm plotting 2 stacked area charts using ggplot2. The data is wait events from an Oracle database. It's a performance tuning chart. I have several questions.
- 下面的两张图不能正确排列,很可能是由于图例中文字的宽度。是否有一个简单的解决方案呢?
- 这两个图是真正相关的,顶部图显示等待类,如CPU和用户I / O,底部图显示这些课程中具体的等待事件的细节。我希望底部的颜色基于等待课程,与顶部相同,只是具体事件的颜色不同。如果你不喜欢这个概念,我也愿意接受其他选择。这是传达很多信息。我已经将事件数量限制为12个以适应配色方案,但是如果可以工作,则会有更多事件。
- 我想要显示更精确的时间滴答X,或者甚至可以将非营业时间(下午6点至8点)设置为灰色,以便更好地表达时间。 有没有超过12种颜色的人的配色方案常用?通过啤酒厂看,这是最大的。
这里是我的代码:
Here's my code:
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)
推荐答案
对齐问题的最简单解决方案可能是以移动传说:
Possibly the easiest solution to the alignment problem is to move the legends around:
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轴上的分辨率,我会使用类似于:
To increase the resolution on the x axis,I'd use something like:
+ scale_x_datetime(breaks = date_breaks("2 hours"))
或任何你喜欢的休息。
使用 geom_rect
和设置 alpha = 0.25
或其他东西来完成特定区域的着色。这需要用rect的开始点和结束点创建一个单独的数据框(使用 Inf
和 -Inf
y坐标)传递给 geom_rect
。
Shading a particular region is typically done with geom_rect
and setting alpha = 0.25
or something. This would require creating a separate data frame with the start and end points of the rect (use Inf
and -Inf
for the y coordinates) to pass to geom_rect
.
这篇关于R - ggplot2,几个问题,多个相关的情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!