gpt4 book ai didi

r - 修改多面图中的图例,ggplot2

转载 作者:行者123 更新时间:2023-12-01 11:45:50 24 4
gpt4 key购买 nike

对于此图,我想将图例拆分为 2 个独立的图例。一种将脉冲湿地显示为圆圈,阴影对应于湿地编号,另一种将静态湿地显示为三角形,灰色阴影对应于湿地编号。然后 Hydrology 传说就没有必要了,我想摆脱它。

这就是我希望图例的样子(但当然更小。这是在 PowerPoint 中完成的,只是为了演示)。

enter image description here

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

Temp graph

数据

      Date Hydrology Wetland Temp   Layer1   15-Jun    Pulsed     One 21.9 Surface2   15-Jun    Pulsed     One 21.0  Bottom3    1-Jul    Pulsed     One 28.8 Surface4    1-Jul    Pulsed     One 23.5  Bottom5   10-Jul    Pulsed     One 31.8 Surface6   10-Jul    Pulsed     One 22.6  Bottom7   14-Jul    Pulsed     One 23.0 Surface8   14-Jul    Pulsed     One 21.4  Bottom9   15-Aug    Pulsed     One 28.8 Surface10  15-Aug    Pulsed     One 28.5  Bottom11   7-Nov    Pulsed     One 12.1 Surface12   7-Nov    Pulsed     One  9.9  Bottom13  15-Jun    Static     Two 22.2 Surface14  15-Jun    Static     Two 21.2  Bottom15   1-Jul    Static     Two 29.0 Surface16   1-Jul    Static     Two 24.1  Bottom17  10-Jul    Static     Two 30.9 Surface18  10-Jul    Static     Two 23.3  Bottom19  14-Jul    Static     Two 25.2 Surface20  14-Jul    Static     Two 22.6  Bottom21   1-Aug    Static     Two 27.3 Surface22   1-Aug    Static     Two 23.5  Bottom23  15-Aug    Static     Two 26.7 Surface24  15-Aug    Static     Two 23.6  Bottom25   7-Nov    Static     Two 11.9 Surface26   7-Nov    Static     Two  9.0  Bottom27  15-Jun    Pulsed   Three 21.3 Surface28  15-Jun    Pulsed   Three 20.9  Bottom29   1-Jul    Pulsed   Three 26.1 Surface30   1-Jul    Pulsed   Three 23.8  Bottom31  10-Jul    Pulsed   Three 30.1 Surface32  10-Jul    Pulsed   Three 22.8  Bottom33  14-Jul    Pulsed   Three 23.7 Surface34  14-Jul    Pulsed   Three 22.2  Bottom35   7-Nov    Pulsed   Three 12.5 Surface36   7-Nov    Pulsed   Three 10.0  Bottom37  15-Jun    Static    Four 21.0 Surface38  15-Jun    Static    Four 19.3  Bottom39   1-Jul    Static    Four 25.0 Surface40   1-Jul    Static    Four 21.2  Bottom41  10-Jul    Static    Four 25.7 Surface42  10-Jul    Static    Four 21.3  Bottom43  14-Jul    Static    Four 22.5 Surface44  14-Jul    Static    Four 20.8  Bottom45   1-Aug    Static    Four 24.1 Surface46   1-Aug    Static    Four 22.8  Bottom47  15-Aug    Static    Four 25.1 Surface48  15-Aug    Static    Four 22.2  Bottom49   7-Nov    Static    Four 12.2 Surface50   7-Nov    Static    Four  9.1  Bottom51  15-Jun    Static    Five 19.7 Surface52  15-Jun    Static    Five 19.2  Bottom53   1-Jul    Static    Five 24.1 Surface54   1-Jul    Static    Five 22.9  Bottom55  10-Jul    Static    Five 24.6 Surface56  10-Jul    Static    Five 21.7  Bottom57  14-Jul    Static    Five 21.3 Surface58  14-Jul    Static    Five 23.0  Bottom59   1-Aug    Static    Five 23.8 Surface60   1-Aug    Static    Five 22.0  Bottom61  15-Aug    Static    Five 24.8 Surface62  15-Aug    Static    Five 23.1  Bottom63   7-Nov    Static    Five 11.7 Surface64   7-Nov    Static    Five  8.8  Bottom65  15-Jun    Pulsed     Six 21.2 Surface66  15-Jun    Pulsed     Six 20.4  Bottom67   1-Jul    Pulsed     Six 24.9 Surface68   1-Jul    Pulsed     Six 22.5  Bottom69  10-Jul    Pulsed     Six 28.6 Surface70  10-Jul    Pulsed     Six 22.0  Bottom71  14-Jul    Pulsed     Six 21.9 Surface72  14-Jul    Pulsed     Six 21.4  Bottom73  15-Aug    Pulsed     Six 27.5 Surface74  15-Aug    Pulsed     Six 26.1  Bottom75   7-Nov    Pulsed     Six 13.3 Surface76   7-Nov    Pulsed     Six  9.5  Bottom77  15-Jun    Pulsed   Seven 19.7 Surface78  15-Jun    Pulsed   Seven 18.7  Bottom79   1-Jul    Pulsed   Seven 23.0 Surface80   1-Jul    Pulsed   Seven 20.8  Bottom81  10-Jul    Pulsed   Seven 24.7 Surface82  10-Jul    Pulsed   Seven 22.2  Bottom83  14-Jul    Pulsed   Seven 21.3 Surface84  14-Jul    Pulsed   Seven 20.6  Bottom85  15-Aug    Pulsed   Seven 26.2 Surface86  15-Aug    Pulsed   Seven 24.7  Bottom87   7-Nov    Pulsed   Seven 12.0 Surface88   7-Nov    Pulsed   Seven 10.5  Bottom89  15-Jun    Static   Eight 20.6 Surface90  15-Jun    Static   Eight 19.5  Bottom91   1-Jul    Static   Eight 28.1 Surface92   1-Jul    Static   Eight 22.5  Bottom93  10-Jul    Static   Eight 28.8 Surface94  10-Jul    Static   Eight 23.1  Bottom95  14-Jul    Static   Eight 24.7 Surface96  14-Jul    Static   Eight 21.9  Bottom97   1-Aug    Static   Eight 25.0 Surface98   1-Aug    Static   Eight 22.0  Bottom99  15-Aug    Static   Eight 27.6 Surface100 15-Aug    Static   Eight 25.3  Bottom101  7-Nov    Static   Eight 12.1 Surface102  7-Nov    Static   Eight 10.3  Bottom103 15-Jun    Static    Nine 20.7 Surface104 15-Jun    Static    Nine 19.8  Bottom105  1-Jul    Static    Nine 24.8 Surface106  1-Jul    Static    Nine 22.7  Bottom107 10-Jul    Static    Nine 28.3 Surface108 10-Jul    Static    Nine 22.8  Bottom109 14-Jul    Static    Nine 23.9 Surface110 14-Jul    Static    Nine 22.0  Bottom111  1-Aug    Static    Nine 27.7 Surface112  1-Aug    Static    Nine 24.5  Bottom113 15-Aug    Static    Nine 25.8 Surface114 15-Aug    Static    Nine 23.4  Bottom115  7-Nov    Static    Nine 11.9 Surface116  7-Nov    Static    Nine  9.5  Bottom117 15-Jun    Pulsed     Ten 22.3 Surface118 15-Jun    Pulsed     Ten 21.9  Bottom119  1-Jul    Pulsed     Ten 29.6 Surface120  1-Jul    Pulsed     Ten 28.2  Bottom121 10-Jul    Pulsed     Ten 33.0 Surface122 10-Jul    Pulsed     Ten 30.4  Bottom123 14-Jul    Pulsed     Ten 25.6 Surface124 14-Jul    Pulsed     Ten 24.1  Bottom125  7-Nov    Pulsed     Ten 13.2 Surface126  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)

谢谢,

-莎拉

最佳答案

这是受此 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 数据的绘图。在 geom_point() 中设置 shape=16。使用 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)

使用 ggplot_build()ggplot_gtable() 保存绘图 p1p2

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

仅提取两个地 block 的图例。这次图例在 12.element(包含 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)))

enter image description here

关于r - 修改多面图中的图例,ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15462203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com