ggplot2:添加背景层

发布于 2024-09-14 18:14:13 字数 2566 浏览 0 评论 0原文

我想将暗/亮相位信息添加到堆叠区域图集的背景中,以突出显示光线如何影响曲线的形状。我的数据框如下所示:

> 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 中编辑了我的图形集以显示我的意思:

my altered graphset

我一直在尝试使用主题,但事实并非如此锻炼得很好。想法?

预计到达时间

所以,使用这个例子(来自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:

my altered graphset

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2024-09-21 18:14:13

使用您更新的代码,我将仅获取黑暗区域的子集。然后添加

geom_rect(data = dark.subset, aes(xmin = phase_start, xmax = phase_end), 
              ymin = -Inf,
              ymax = Inf,
              fill = alpha("#000000", 0.2))

这将避免将lightdark映射到任何比例。至于出现在顶部,请以不同的顺序添加几何图形:

ggplot(...)+
  geom_rect(...)+
  geom_area(...)

With your updated code, I would take a subset of just the dark regions. Then add

geom_rect(data = dark.subset, aes(xmin = phase_start, xmax = phase_end), 
              ymin = -Inf,
              ymax = Inf,
              fill = alpha("#000000", 0.2))

That will avoid mapping the light and dark to any scales. As for appearing on top, add your geoms in a different order:

ggplot(...)+
  geom_rect(...)+
  geom_area(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文