修改小平面图ggplot2中的图例 [英] Modify legend in facetted plot, ggplot2

查看:148
本文介绍了修改小平面图ggplot2中的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这张图,我想将传说分成两个独立的传说。一个将脉动湿地显示为与湿地编号对应的阴影,一个将静态湿地显示为具有对应于湿地编号的灰色阴影的三角形。然后水文学传奇将不是必要的,我想摆脱这一点。



这是我想让图例看起来像(但是更小,当然这是在PowerPoint中完成演示)。





我粘贴下面的数据和代码。





数据

 
日期水文湿地临时层
1 15-Jun Pulsed One 21.9 Surface
2 15-Jun Pulsed One 21.0 Bottom
3 1-Jul Pulsed One 28.8 Surface
4 1-Jul Pulsed One 23.5 Bottom
5 10-Jul Pulsed One 31.8表面
6 10-Jul脉冲一个22.6底部
7 14-Jul脉冲一个23.0表面
8 14-Jul脉冲一个21.4底部
9 15 - 脉冲一个28.8表面
10 15-Aug脉冲一个28.5底部
11 7-Nov脉冲一个12.1表面
12 7月11日脉冲1 9.9底部
13 15-Jun静态两个22.2表面
14 15-Jun静态两个21.2底部
15 1-Jul静态两个29.0表面
16 1-Jul Static Two 24.1底部
17 7月7日静态2 30.9表面
18 7月7日静态2 23.3底部
19 7月14日静态2 25.2表面
20 14- Jul静态两个22.6底部
21 1月8日静态两个27.3表面
22 1月8日静态两个23.5底部
23 15年8月静态两个26.7表面
24 15年8月静态两个23.6底部
25 7月11日静态两个11.9表面
26 7月11日静态两个9.0底部
27 15-Jun脉冲三个21.3表面
28 15-Jun脉冲三20.9底部
29 1-Jul脉冲三个26.1表面
30 1-Jul脉冲三个23.8底部
31 10-Jul脉冲三个30.1表面
32 10-Jul脉冲三个22.8底部
33 14-Jul脉冲三23.7表面
34 14-Jul脉冲三22.2底部
35 7-Nov脉冲三12.5表面
36 7-Nov脉冲三10.0底部
37 15-Jun Static Four 21.0表面
38 15-Jun静态四19.3底部
39 1-Jul静态四25.0表面
40 1-Jul静态四21.2底部
41 10-Jul静态四25.7表面
42 7月7日静态四21.3底部
43 14-Jul静态四22.5表面$ b $ 44 44 7月静态四20.8底部
45 1月8日静态四24.1表面
46 1月8日静态4 22.8底部
47 15年8月静态四25.1表面
48 15-Aug静态四22.2底部
49 11月7日静态四12.2表面
50 7月11日静态四点9.1底部
51 15-Jun静态五19.7表面
52 15-Jun静态五19.2底部
53 1-Jul静态五24.1表面
54 1 -Jul Static Five 22.9底部
55 10月7日静态5 24.6表面
56 7月7日静态5 21.7底部
57 7月14日静态5 21.3表面$ b $ 58 58 7月静态5 23.0底部
59 1月8日静态5 23.8表面
60 1月8日静态5 22.0底部
61 15-Aug Static Five 24.8表面
62 15-Aug Static Five 23.1底部
63 7月11日静态5 11.7表面
64 7月11日静态五8.8底部
65 15-Jun脉冲六6 21.2表面
66 15-Jun脉冲六20.4底部
67 1月7日脉冲式6个24.9表面
68 1月7日脉冲式6个22.5个底部
69个10月7个脉冲式六个28.6表面
70 10-Jul脉冲式六个22.0个底部
71 2009年7月14日脉冲式六21.9表面
72 14-Jul脉冲式六21.4底部$ b $ 73 73 8月脉冲式六27.5表面
74 15-Aug脉冲式六26.1底部
75 7- 11月脉冲六13.3苏尔面对
76 7月11日脉冲六点9.5底部
77 15-Jun脉冲七19.7表面
78 15-Jun脉冲七18.7底部
79 1-Jul脉冲7 23.0表面
80 1-Jul脉冲7 20.8底部
81 10-Jul脉冲7 24.7表面
82 10-Jul脉冲7七22.2底部
83 14-Jul脉冲七21.3表面
84 14-Jul脉冲7 20.6底部
85 15-Aug脉冲7 26.2表面
86 15-Aug脉冲七24.7底部
87 7-Nov脉冲7 12.0表面
88 7-Nov Pulsed Seven 10.5底部
89 15-Jun静态八20.6表面
90 15-Jun静态八19.5底部
91 1-Jul静态八28.1表面
92 1- Jul静态八22.5底部
93 10-Jul静态八28.8表面
94 7-Jul-8静态八23.1底部
14-Jul静态八24.7表面
96静态四月14,八个21.9底部
97 1月8日静态八25.0表面
98 1月8日静态八22.0底部
99 15-Aug静态八27.6表面
100 15-Aug静态八8 25.3底部
101 7- 11月静态八12.1表面
102 7月11日静态八10.3底部
103 15-Jun静态九20.7表面
104 15-Jun静态九19.8底部
105 1-Jul Static九24.8表面
106 1-Jul静态九22.7底部
107 7-Jul-7静态九28.3表面
108-Jul-7静态九22.8底部
109十二月静态九23.9表面
110 14-Jul静态九22.0底部
111 1月8日静态九27.7表面
112 1月8日静态九24.5底部
113 15-Aug静态九25.8表面
114 15年8月静态9 23.4底部$ b $ 115 115 11月静态9.9 11.9表面
116 7月11日静态9 9.5底部
117 15-Jun脉冲十10.3表面
118 15-Jun Pu lsed 10 21.9底部
119 1月7月脉冲10 29.6表面
120 1月7月脉冲10 28.2底部
121 10月7日脉冲10 10 33月表面
122 10-Jul脉冲十30.4底部
123 14-Jul脉冲10 25.6表面
124 14-Jul脉动10 24.1底部
125 7-Nov脉动10 13.2表面
126 7-Nov脉动10 10.1底部

代码

  colvec< -c(white,white,gray80,gray80,gray60,gray60,gray37,gray37,black,black)
water $ Layer =因子(水$ Layer,c(Surface,Bottom))
water $ Date = factor(water $ Date,c(15-Jun,1-7-Jul, ),7月14日,1月8日,8月15日,7月11日))
water $湿地=因子(水$湿地,c(One ,三,四,五,六,七,八,九,十))

ggplot(water,aes(Date, Temp,group = Wetland,shape = Hydrology))+
geom_point(size = 4,color =black)+
geom_point( aes(color = Wetland),size = 3)+
scale_colour_manual(values = colvec)+
facet_grid(Layer〜Hydrology)

谢谢,

sarah

解决方案

这是一个解决方案,其灵感来自于问题



首先,创建一个只包含一次所有颜色的新变量 colvec2

  colvec2 <-c(white,gray80,gray60,gray37,black)
colvec< -c(white,white,gray80,gray80,gray60,gray60,gray37,gray37,black,black)

保存为对象且没有图例的原始图。



<$ (水,aes(日期,温度,组=湿地,形状=水文))+
geom_point(aes(颜色=湿地),尺寸= 3) +
scale_colour_manual(values = colvec)+
facet_grid(Layer 〜水文)+
主题(legend.position =none)

包含只有 Pulsed 的数据。在 geom_point()中设置 shape = 16

  p1 < ggplot(子集(水,水文==脉冲),
aes(日期,温度,组=湿地,形状=水文))+
geom_point(aes(color = Wetland),size = 3 ,shape = 16)+
scale_colour_manual(Pulsed Wetlands,values = colvec2)+
facet_grid(Layer〜Hydrology)

p1 相同,仅适用于静态



pre $ p2 <-ggplot(子集(water,Hydrology ==Static),
aes(Date,Temp,group = Wetland (aes(color = Wetland),size = 3,shape = 17)+
scale_colour_manual(静态湿地,值= colvec2)+
facet_grid (Layer〜Hydrology)

保存地块 p1 p2 使用 ggplot_build() ggplot_gtable()

  pt1 <-ggplot_gtable(ggplot_build(p1))
pt2 <-ggplot_gtable(gg plot_build(p2))

提取这两个图的传说只是grob。这个时间的传说在12.元素(元素包含 guide-box

  leg1< -pt1 $ grobs [[12]] 
leg2< -pt2 $ grobs [[12]]

现在使用 grid.arrange() arrangeGrob()来绘制所有零件。 / p>

  library(gridExtra)
grid.arrange(arrangeGrob(p,arrangeGrob(leg1,leg2),ncol = 2, widths = c(3 / 4,1 / 4)))


For this graph, I want to split the legend into 2 separate legends. One showing the Pulsed Wetlands as circles with the shades corresponding to Wetland number and one showing the Static Wetlands as triangles with the gray shades corresponding to Wetland number. And then the Hydrology legend wouldn't be necessary and I'd like to get rid of that.

Here's what I want the legend to look like (but smaller, of course. This was done in PowerPoint just for demonstration).

I'm pasting the data and the code below.

Data

      Date Hydrology Wetland Temp   Layer
1   15-Jun    Pulsed     One 21.9 Surface
2   15-Jun    Pulsed     One 21.0  Bottom
3    1-Jul    Pulsed     One 28.8 Surface
4    1-Jul    Pulsed     One 23.5  Bottom
5   10-Jul    Pulsed     One 31.8 Surface
6   10-Jul    Pulsed     One 22.6  Bottom
7   14-Jul    Pulsed     One 23.0 Surface
8   14-Jul    Pulsed     One 21.4  Bottom
9   15-Aug    Pulsed     One 28.8 Surface
10  15-Aug    Pulsed     One 28.5  Bottom
11   7-Nov    Pulsed     One 12.1 Surface
12   7-Nov    Pulsed     One  9.9  Bottom
13  15-Jun    Static     Two 22.2 Surface
14  15-Jun    Static     Two 21.2  Bottom
15   1-Jul    Static     Two 29.0 Surface
16   1-Jul    Static     Two 24.1  Bottom
17  10-Jul    Static     Two 30.9 Surface
18  10-Jul    Static     Two 23.3  Bottom
19  14-Jul    Static     Two 25.2 Surface
20  14-Jul    Static     Two 22.6  Bottom
21   1-Aug    Static     Two 27.3 Surface
22   1-Aug    Static     Two 23.5  Bottom
23  15-Aug    Static     Two 26.7 Surface
24  15-Aug    Static     Two 23.6  Bottom
25   7-Nov    Static     Two 11.9 Surface
26   7-Nov    Static     Two  9.0  Bottom
27  15-Jun    Pulsed   Three 21.3 Surface
28  15-Jun    Pulsed   Three 20.9  Bottom
29   1-Jul    Pulsed   Three 26.1 Surface
30   1-Jul    Pulsed   Three 23.8  Bottom
31  10-Jul    Pulsed   Three 30.1 Surface
32  10-Jul    Pulsed   Three 22.8  Bottom
33  14-Jul    Pulsed   Three 23.7 Surface
34  14-Jul    Pulsed   Three 22.2  Bottom
35   7-Nov    Pulsed   Three 12.5 Surface
36   7-Nov    Pulsed   Three 10.0  Bottom
37  15-Jun    Static    Four 21.0 Surface
38  15-Jun    Static    Four 19.3  Bottom
39   1-Jul    Static    Four 25.0 Surface
40   1-Jul    Static    Four 21.2  Bottom
41  10-Jul    Static    Four 25.7 Surface
42  10-Jul    Static    Four 21.3  Bottom
43  14-Jul    Static    Four 22.5 Surface
44  14-Jul    Static    Four 20.8  Bottom
45   1-Aug    Static    Four 24.1 Surface
46   1-Aug    Static    Four 22.8  Bottom
47  15-Aug    Static    Four 25.1 Surface
48  15-Aug    Static    Four 22.2  Bottom
49   7-Nov    Static    Four 12.2 Surface
50   7-Nov    Static    Four  9.1  Bottom
51  15-Jun    Static    Five 19.7 Surface
52  15-Jun    Static    Five 19.2  Bottom
53   1-Jul    Static    Five 24.1 Surface
54   1-Jul    Static    Five 22.9  Bottom
55  10-Jul    Static    Five 24.6 Surface
56  10-Jul    Static    Five 21.7  Bottom
57  14-Jul    Static    Five 21.3 Surface
58  14-Jul    Static    Five 23.0  Bottom
59   1-Aug    Static    Five 23.8 Surface
60   1-Aug    Static    Five 22.0  Bottom
61  15-Aug    Static    Five 24.8 Surface
62  15-Aug    Static    Five 23.1  Bottom
63   7-Nov    Static    Five 11.7 Surface
64   7-Nov    Static    Five  8.8  Bottom
65  15-Jun    Pulsed     Six 21.2 Surface
66  15-Jun    Pulsed     Six 20.4  Bottom
67   1-Jul    Pulsed     Six 24.9 Surface
68   1-Jul    Pulsed     Six 22.5  Bottom
69  10-Jul    Pulsed     Six 28.6 Surface
70  10-Jul    Pulsed     Six 22.0  Bottom
71  14-Jul    Pulsed     Six 21.9 Surface
72  14-Jul    Pulsed     Six 21.4  Bottom
73  15-Aug    Pulsed     Six 27.5 Surface
74  15-Aug    Pulsed     Six 26.1  Bottom
75   7-Nov    Pulsed     Six 13.3 Surface
76   7-Nov    Pulsed     Six  9.5  Bottom
77  15-Jun    Pulsed   Seven 19.7 Surface
78  15-Jun    Pulsed   Seven 18.7  Bottom
79   1-Jul    Pulsed   Seven 23.0 Surface
80   1-Jul    Pulsed   Seven 20.8  Bottom
81  10-Jul    Pulsed   Seven 24.7 Surface
82  10-Jul    Pulsed   Seven 22.2  Bottom
83  14-Jul    Pulsed   Seven 21.3 Surface
84  14-Jul    Pulsed   Seven 20.6  Bottom
85  15-Aug    Pulsed   Seven 26.2 Surface
86  15-Aug    Pulsed   Seven 24.7  Bottom
87   7-Nov    Pulsed   Seven 12.0 Surface
88   7-Nov    Pulsed   Seven 10.5  Bottom
89  15-Jun    Static   Eight 20.6 Surface
90  15-Jun    Static   Eight 19.5  Bottom
91   1-Jul    Static   Eight 28.1 Surface
92   1-Jul    Static   Eight 22.5  Bottom
93  10-Jul    Static   Eight 28.8 Surface
94  10-Jul    Static   Eight 23.1  Bottom
95  14-Jul    Static   Eight 24.7 Surface
96  14-Jul    Static   Eight 21.9  Bottom
97   1-Aug    Static   Eight 25.0 Surface
98   1-Aug    Static   Eight 22.0  Bottom
99  15-Aug    Static   Eight 27.6 Surface
100 15-Aug    Static   Eight 25.3  Bottom
101  7-Nov    Static   Eight 12.1 Surface
102  7-Nov    Static   Eight 10.3  Bottom
103 15-Jun    Static    Nine 20.7 Surface
104 15-Jun    Static    Nine 19.8  Bottom
105  1-Jul    Static    Nine 24.8 Surface
106  1-Jul    Static    Nine 22.7  Bottom
107 10-Jul    Static    Nine 28.3 Surface
108 10-Jul    Static    Nine 22.8  Bottom
109 14-Jul    Static    Nine 23.9 Surface
110 14-Jul    Static    Nine 22.0  Bottom
111  1-Aug    Static    Nine 27.7 Surface
112  1-Aug    Static    Nine 24.5  Bottom
113 15-Aug    Static    Nine 25.8 Surface
114 15-Aug    Static    Nine 23.4  Bottom
115  7-Nov    Static    Nine 11.9 Surface
116  7-Nov    Static    Nine  9.5  Bottom
117 15-Jun    Pulsed     Ten 22.3 Surface
118 15-Jun    Pulsed     Ten 21.9  Bottom
119  1-Jul    Pulsed     Ten 29.6 Surface
120  1-Jul    Pulsed     Ten 28.2  Bottom
121 10-Jul    Pulsed     Ten 33.0 Surface
122 10-Jul    Pulsed     Ten 30.4  Bottom
123 14-Jul    Pulsed     Ten 25.6 Surface
124 14-Jul    Pulsed     Ten 24.1  Bottom
125  7-Nov    Pulsed     Ten 13.2 Surface
126  7-Nov    Pulsed     Ten 10.1  Bottom 

Code

colvec <-c("white", "white","gray80", "gray80", "gray60", "gray60", "gray37", "gray37","black", "black")  
water$Layer=factor(water$Layer, c("Surface", "Bottom"))
water$Date=factor(water$Date, c("15-Jun", "1-Jul", "10-Jul", "14-Jul", "1-Aug", "15-Aug", "7-Nov"))
water$Wetland=factor(water$Wetland, c("One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"))

ggplot(water, aes(Date, Temp, group=Wetland, shape=Hydrology)) +
  geom_point(size =4, color = "black")+
  geom_point(aes(color=Wetland),size=3) +
  scale_colour_manual(values=colvec) +
  facet_grid(Layer ~ Hydrology) 

Thanks,

-Sarah

解决方案

Here is a solution inspired by this question

First, made new variable colvec2 that contains all colors only once.

colvec2 <-c("white", "gray80", "gray60", "gray37",  "black")  
colvec <-c("white", "white","gray80", "gray80", "gray60", "gray60", "gray37", "gray37","black", "black")  

Original plot saved as object and without the legend.

p<-ggplot(water, aes(Date, Temp, group=Wetland, shape=Hydrology)) +
  geom_point(aes(color=Wetland),size=3) +
  scale_colour_manual(values=colvec) +
  facet_grid(Layer ~ Hydrology) +
  theme(legend.position="none")

Plot that contains only data of Pulsed. Set shape=16 inside the geom_point(). With scale_colour_manual() changed legend title and colors.

p1<-ggplot(subset(water,Hydrology=="Pulsed"), 
     aes(Date, Temp, group=Wetland, shape=Hydrology)) +
  geom_point(aes(color=Wetland),size=3,shape=16) +
  scale_colour_manual("Pulsed Wetlands",values=colvec2) +
  facet_grid(Layer ~ Hydrology)

The same as p1, only for Static.

p2<-ggplot(subset(water,Hydrology=="Static"), 
      aes(Date, Temp, group=Wetland, shape=Hydrology)) +
  geom_point(aes(color=Wetland),size=3,shape=17) +
  scale_colour_manual("Static Wetlands",values=colvec2) +
  facet_grid(Layer ~ Hydrology)

Save plots p1 and p2 using ggplot_build() and ggplot_gtable().

pt1<-ggplot_gtable(ggplot_build(p1))
pt2<-ggplot_gtable(ggplot_build(p2))

Extract just grob of legends for both plots. This time legends are in 12. element (element containing guide-box)

leg1<-pt1$grobs[[12]]
leg2<-pt2$grobs[[12]]

Now use grid.arrange() and arrangeGrob() to plot all parts.

library(gridExtra)
grid.arrange(arrangeGrob(p,arrangeGrob(leg1,leg2), ncol = 2,widths=c(3/4,1/4)))

这篇关于修改小平面图ggplot2中的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆