ggplot2:添加背景层
我想将暗/亮相位信息添加到堆叠区域图集的背景中,以突出显示光线如何影响曲线的形状。我的数据框如下所示:
> str(MDist.median)
'data.frame': 2880 obs. of 6 variables:
$ groupname: Factor w/ 8 levels "rowA","rowB",..: 1 1 1 1 1 1 1 1 1 1 ...
$ fCycle : Factor w/ 6 levels "predark","Cycle 1",..: 1 1 1 1 1 1 1 1 1 1 ...
$ fPhase : Factor w/ 2 levels "Light","Dark": 2 2 2 2 2 2 2 2 2 2 ...
$ starttime: num 0.3 60 120 180 240 300 360 420 480 540 ...
$ dists : Factor w/ 3 levels "inadist","smldist",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 110 123 124 128 132 ...
示例数据:
> head(MDist.median)
groupname fCycle fPhase starttime dists value
1 rowA predark Dark 0.3 inadist 110.00
2 rowA predark Dark 60.0 inadist 123.25
3 rowA predark Dark 120.0 inadist 124.10
4 rowA predark Dark 180.0 inadist 128.35
5 rowA predark Dark 240.0 inadist 131.80
6 rowA predark Dark 300.0 inadist 140.30
我的面积图通过 x 轴上的开始时间在 y 轴上堆叠 3 种距离。
dists.med.areaplot <- qplot(starttime,value,fill=dists,facets=~groupname,
geom='area',data=MDist.median, stat='identity') +
labs(y='median distances', x='time(s)', fill='Distance Types')+
opts(title='Changes in Fish Activity and Activity Type') +
scale_fill_brewer(type='seq')
我想在每个图后面添加垂直矩形,这些矩形按每个 fCycle 的最小和最大开始时间水平定位,其中 fPhase==“Dark”。我在 GIMP 中编辑了我的图形集以显示我的意思:
我一直在尝试使用主题,但事实并非如此锻炼得很好。想法?
预计到达时间
所以,使用这个例子(来自Hadley的网站):
(unemp <- qplot(date, unemploy, data=economics, geom="line",
xlab = "", ylab = "No. unemployed (1000s)"))
presidential <- presidential[-(1:3), ]
yr <- range(economics$unemploy)
unemp + geom_rect(
aes(NULL, NULL, xmin = start, xmax = end, fill = party),
ymin = yr[1], ymax = yr[2], data = presidential) +
scale_fill_manual(values = alpha(c("blue", "red"), 0.2)
)
我可以这样做我的数据:
dists.med.areaplot + geom_rect(
aes(NULL, NULL, xmin = phase_start, xmax = phase_end, fill = fPhase),
ymin = -Inf, ymax = Inf, data = phase_starts) +
scale_fill_manual(values = alpha(c("#cccccc", "#000000"), 0.2)
)
它将浅色和深色类别添加到图例中,并将所有颜色更改为灰色。
如果我省略了scale_fill_manual(values = alpha(c("#cccccc", "#000000"), 0.2),我会得到我期望的矩形,但图例仍然会发生变化,但它们完全是不透明,在我的原始图表之上,并使用我的颜色序列中的第 1 2 个颜色。
如何在原始图后面制作 geom_rect ?
I want to added dark/light phase information to the background of my stacked area graphset to highlight the how light affects the shapes of the curves. My dataframe looks like this:
> str(MDist.median)
'data.frame': 2880 obs. of 6 variables:
$ groupname: Factor w/ 8 levels "rowA","rowB",..: 1 1 1 1 1 1 1 1 1 1 ...
$ fCycle : Factor w/ 6 levels "predark","Cycle 1",..: 1 1 1 1 1 1 1 1 1 1 ...
$ fPhase : Factor w/ 2 levels "Light","Dark": 2 2 2 2 2 2 2 2 2 2 ...
$ starttime: num 0.3 60 120 180 240 300 360 420 480 540 ...
$ dists : Factor w/ 3 levels "inadist","smldist",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 110 123 124 128 132 ...
example data:
> head(MDist.median)
groupname fCycle fPhase starttime dists value
1 rowA predark Dark 0.3 inadist 110.00
2 rowA predark Dark 60.0 inadist 123.25
3 rowA predark Dark 120.0 inadist 124.10
4 rowA predark Dark 180.0 inadist 128.35
5 rowA predark Dark 240.0 inadist 131.80
6 rowA predark Dark 300.0 inadist 140.30
My area plot stacks the 3 kinds of dists
on the y axis by starttime on the x axis.
dists.med.areaplot <- qplot(starttime,value,fill=dists,facets=~groupname,
geom='area',data=MDist.median, stat='identity') +
labs(y='median distances', x='time(s)', fill='Distance Types')+
opts(title='Changes in Fish Activity and Activity Type') +
scale_fill_brewer(type='seq')
I want to add vertical rectangles behind each plot that are horizontally positioned by the min and max starttimes of each fCycle where fPhase=="Dark". I edited my graphset in GIMP to show what I mean:
I've been attempting to use themes but that's not working out so well. Thoughts?
ETA
So, using this example (from Hadley's website):
(unemp <- qplot(date, unemploy, data=economics, geom="line",
xlab = "", ylab = "No. unemployed (1000s)"))
presidential <- presidential[-(1:3), ]
yr <- range(economics$unemploy)
unemp + geom_rect(
aes(NULL, NULL, xmin = start, xmax = end, fill = party),
ymin = yr[1], ymax = yr[2], data = presidential) +
scale_fill_manual(values = alpha(c("blue", "red"), 0.2)
)
I can do this with my data:
dists.med.areaplot + geom_rect(
aes(NULL, NULL, xmin = phase_start, xmax = phase_end, fill = fPhase),
ymin = -Inf, ymax = Inf, data = phase_starts) +
scale_fill_manual(values = alpha(c("#cccccc", "#000000"), 0.2)
)
which adds Light and Dark categories to the legend and changes ALL the colours to grey.
If I leave out the scale_fill_manual(values = alpha(c("#cccccc", "#000000"), 0.2)
, I get the rectangles I expect still with the legend change, but they are completely opaque, on top of my original graph, and use the 1st 2 colours in my colour sequence.
How can I make the geom_rect behind the original plots?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用您更新的代码,我将仅获取
黑暗
区域的子集。然后添加这将避免将
light
和dark
映射到任何比例。至于出现在顶部,请以不同的顺序添加几何图形:With your updated code, I would take a subset of just the
dark
regions. Then addThat will avoid mapping the
light
anddark
to any scales. As for appearing on top, add your geoms in a different order: