对于此图表,我想将图例拆分为2个单独的图例。一个显示脉冲湿地为圆形,阴影对应湿地编号,一个显示静态湿地为三角形,灰色阴影对应湿地编号。然后水文学的传说就没有必要了,我想摆脱它。
这就是我希望传奇看起来像(但当然更小。这只是在PowerPoint中用于演示)。
我正在粘贴下面的数据和代码。
数据
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
代码
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)
谢谢,
-Sarah
答案 0 :(得分:5)
这是一个受此question
启发的解决方案首先,制作了包含所有颜色一次的新变量colvec2
。
colvec2 <-c("white", "gray80", "gray60", "gray37", "black")
colvec <-c("white", "white","gray80", "gray80", "gray60", "gray60", "gray37", "gray37","black", "black")
原始图表保存为对象且没有图例。
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")
仅包含Pulsed
数据的图表。在shape=16
内设置geom_point()
。 scale_colour_manual()
更改了图例标题和颜色。
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)
与p1
相同,仅适用于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)
使用p1
和p2
保存地图ggplot_build()
和ggplot_gtable()
。
pt1<-ggplot_gtable(ggplot_build(p1))
pt2<-ggplot_gtable(ggplot_build(p2))
提取两个地块的传说。这一段时间的图例是12.元素(包含guide-box
的元素)
leg1<-pt1$grobs[[12]]
leg2<-pt2$grobs[[12]]
现在使用grid.arrange()
和arrangeGrob()
绘制所有部分。
library(gridExtra)
grid.arrange(arrangeGrob(p,arrangeGrob(leg1,leg2), ncol = 2,widths=c(3/4,1/4)))