R ggplot2:添加背景层
我想将暗/光相位信息添加到堆叠区域图形集的背景中,以突出显示灯光如何影响曲线的形状。我的数据框如下所示:R ggplot2:添加背景层,r,ggplot2,R,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 &qu
> 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
我的面积图将3种dist
按starttime在x轴上堆叠在y轴上
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')
这为图例添加了明暗两种颜色,并将所有颜色更改为灰色
如果我省略了scale\u fill\u手册(值=alpha(c(“#cccccc”),“#000000”),0.2)
,我得到的矩形仍然是图例更改后的矩形,但它们完全不透明,位于原始图形的顶部,并使用颜色序列中的前两种颜色
如何在原始绘图后绘制几何图形?使用您更新的代码,我将仅获取
暗
区域的子集。然后添加
geom_rect(data = dark.subset, aes(xmin = phase_start, xmax = phase_end),
ymin = -Inf,
ymax = Inf,
fill = alpha("#000000", 0.2))
这将避免将亮
和暗
映射到任何比例。至于显示在顶部,请按不同的顺序添加几何图形:
ggplot(...)+
geom_rect(...)+
geom_area(...)
提示:
geom\u rect
,ymin=-Inf
,ymax=Inf
。另见ggplot2书中的5.10。@hadley:谢谢你的提示。我正在尝试myplot+geom\u rect(aes(NULL,NULL,xmin=phase\u start,xmax=phase\u end,fill=fpphase),ymin=-Inf,ymax=Inf,data=phase\u start))
它正在成功生成正确的矩形。但矩形在顶部,不透明。
geom_rect(data = dark.subset, aes(xmin = phase_start, xmax = phase_end),
ymin = -Inf,
ymax = Inf,
fill = alpha("#000000", 0.2))
ggplot(...)+
geom_rect(...)+
geom_area(...)