R语言 如何将图例拆分为两行?

0lvr5msh  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(577)
Nu  Cd_ppm  pH
1   0.024   5.51
2   0.023   5.53
3   0.024   5.60
4   0.025   5.60
5   0.025   6.41
6   0.024   6.38
7   0.024   6.42
8   0.026   6.39
9   0.016   6.45
10  0.015   6.46
11  0.016   6.46
12  0.014   6.44
13  0.018   7.00
14  0.017   7.10
15  0.017   7.05
16  0.023   7.01
17  0.030   6.80
18  0.034   6.88
19  0.030   6.87
20  0.035   6.89
21  0.024   6.21
22  0.029   6.18
23  0.028   6.21
24  0.031   6.21
25  0.019   6.43
26  0.023   6.38
27  0.021   6.41
28  0.029   6.39
29  0.027   6.98
30  0.025   6.91
31  0.026   6.71
32  0.029   6.67
33  0.037   6.45
34  0.039   6.41
35  0.038   6.48
36  0.037   6.48
37  0.047   6.42
38  0.043   6.36
39  0.051   6.36
40  0.048   6.40
41  0.033   5.95
42  0.036   6.02
43  0.038   5.96
44  0.038   5.95
45  0.041   6.13
46  0.042   6.14
47  0.040   6.13
48  0.048   6.15
49  NA  NA
50  0.006   6.02
51  0.007   6.06
52  0.007   5.99
53  0.080   5.93
54  0.088   5.89
55  0.079   5.86
56  0.079   5.80
57  0.053   7.84
58  0.051   7.87
59  0.069   7.93
60  0.052   7.95
61  0.046   6.04
62  0.048   5.98
63  0.038   6.28
64  0.045   6.54
65  0.176   6.59
66  0.172   6.14
67  0.176   6.38
68  0.176   6.60
69  0.113   6.10
70  0.116   6.14
71  0.114   6.13
72  0.111   6.11
73  0.095   7.06
74  0.065   7.05
75  0.084   7.03
76  0.063   7.01
77  0.048   7.18
78  0.053   7.16
79  0.052   7.28
80  0.051   7.11
81  0.045   7.61
82  0.038   7.62
83  0.046   7.59
84  0.046   7.60
85  0.025   7.51
86  0.026   7.53
87  0.029   7.49
88  0.030   7.54
89  0.030   6.82
90  0.030   6.78
91  0.031   6.82
92  0.031   6.80
93  0.075   6.95
94  0.079   6.90
95  0.076   6.95
96  0.079   6.93
97  0.059   7.39
98  0.065   7.37
99  0.059   7.42
100 0.061   7.41
101 0.038   7.08
102 0.042   7.14
103 0.049   7.24
104 0.058   7.12
105 0.063   7.11
106 0.068   7.11
107 0.058   7.03
108 0.059   7.13
109 0.072   6.67
110 0.076   6.56
111 0.071   6.66
112 0.072   6.59
113 0.113   6.86
114 0.123   6.87
115 0.117   6.81
116 0.114   6.91
117 0.093   6.71
118 0.091   6.71
119 0.090   6.75
120 0.090   6.70
121 0.087   6.63
122 0.099   6.49
123 0.098   6.52
124 0.099   6.45
125 0.010   8.07
126 0.009   8.01
127 0.010   8.06
128 0.009   8.06

my.formula= y ~ exp(1.5*-x)
Pot=data[c(1:52),]
Pot1=data[c(53:64),]
Incub=data[c(65:76),]
Incub1=data[c(77:128),]
library(gridExtra)
library(tidyverse)
library(reshape2)
library(dplyr)
library(reshape2)
library(tidyr)
library(ggpmisc)
library(ggpubr)
library(patchwork) # combine plots
library(gapminder)

p1=ggplot(data=data, aes(x=pH,y=Cd_ppm,col="+Pl (l, control)", col="-Pl (l, control)"))+
  
  geom_point( size=3.5, data = Pot, shape=16)+labs(x="pH", y=expression(Cd~~~mg~(kg~soil)^{-1}))+
  geom_point(size=3.5, data=Pot1,  shape=17)+
  geom_point(size=3.5, data=Incub,  shape=2)+
  geom_point(size=3.5, data=Incub1,  shape=1)+
  geom_smooth(data = subset(data, Nu<53),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95, size=0.5,aes(col="+Pl ( Compost, FS, Fe)"))+
  geom_smooth(data = subset(data, Nu %in% c(53:64)),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95,size=0.5,linetype="dashed", col="black")+
  geom_smooth(data = subset(data, Nu %in% c(65:76)),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95,size=0.5,linetype="dashed", col="black")+
  geom_smooth(data = subset(data, Nu>76),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95, size=0.5,aes(col="-Pl ( Compost, FS, Fe)"))+
  stat_poly_eq(data = subset(data, Nu<53),formula= y ~ exp(1.5*-x),aes(label=paste(..rr.label..)),  
               label.x.npc = "left", label.y.npc = 0.03,  parse=T, size=6, col="black")+
  stat_poly_eq(data = subset(data, Nu=53:64),formula= y ~ exp(1.5*-x),aes(label=paste(..rr.label..)),  
               label.x.npc = "left", label.y.npc = 0.1,  parse=T, size=6, col="black")+
  
  stat_poly_eq(data = subset(data, Nu>76),formula= y ~ exp(1.5*-x),aes(label=paste( ..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.8, parse=T, size=6, col="black", vjust=2, show.legend=F)+
  stat_poly_eq(data = subset(data, Nu=65:76),formula= y ~ exp(1.5*-x),aes(label=paste(..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.9, parse=T, size=6, col="black", vjust=2, show.legend=F)+
   theme(legend.text = element_text(colour="black", size=17,face=1))+
  theme(legend.position = "bottom") +
  guides(color = guide_legend(nrow = 2, byrow = T))+
  scale_colour_manual(labels=c(col="Pot liming", col="Incubation liming", col="Incubation", col="Pot"),values = c("black", "black", "black", "black"))+
  guides(col = guide_legend(override.aes = list(linetype=c(0,0), shape = c(1, 2, 16, 17), color="black")))

png(file="grafic1.png",height = 125, width = 150, unit ="mm", res = 300)
(mfrow=c(2,2))
ggarrange(p1,  ncol=1, nrow=1, common.legend = T, legend="bottom")
dev.off()

您好,我正在尝试将图中的图例拆分为两行。但它不起作用。如果有人给我写一个解决方案,我将非常感谢任何建议。图中的图例由一行组成,我想将其拆分为两行。

7hiiyaii

7hiiyaii1#

您可以在guides函数中使用nrow,如下所示:

library(gridExtra)
library(tidyverse)
library(dplyr)
library(reshape2)
library(tidyr)
library(ggpmisc)
library(ggpubr)
library(patchwork) # combine plots
library(gapminder)

p1=ggplot(data=data, aes(x=pH,y=Cd_ppm,col="+Pl (l, control)", col="-Pl (l, control)"))+
  
  geom_point( size=3.5, data = Pot, shape=16)+labs(x="pH", y=expression(Cd~~~mg~(kg~soil)^{-1}))+
  geom_point(size=3.5, data=Pot1,  shape=17)+
  geom_point(size=3.5, data=Incub,  shape=2)+
  geom_point(size=3.5, data=Incub1,  shape=1)+
  geom_smooth(data = subset(data, Nu<53),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95, size=0.5,aes(col="+Pl ( Compost, FS, Fe)"))+
  geom_smooth(data = subset(data, Nu %in% c(53:64)),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95,size=0.5,linetype="dashed", col="black")+
  geom_smooth(data = subset(data, Nu %in% c(65:76)),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95,size=0.5,linetype="dashed", col="black")+
  geom_smooth(data = subset(data, Nu>76),formula= y ~ exp(1.5*-x), method = "lm", se=F, level=0.95, size=0.5,aes(col="-Pl ( Compost, FS, Fe)"))+
  stat_poly_eq(data = subset(data, Nu<53),formula= y ~ exp(1.5*-x),aes(label=paste(..rr.label..)),  
               label.x.npc = "left", label.y.npc = 0.03,  parse=T, size=6, col="black")+
  stat_poly_eq(data = subset(data, Nu=53:64),formula= y ~ exp(1.5*-x),aes(label=paste(..rr.label..)),  
               label.x.npc = "left", label.y.npc = 0.1,  parse=T, size=6, col="black")+
  
  stat_poly_eq(data = subset(data, Nu>76),formula= y ~ exp(1.5*-x),aes(label=paste( ..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.8, parse=T, size=6, col="black", vjust=2, show.legend=F)+
  stat_poly_eq(data = subset(data, Nu=65:76),formula= y ~ exp(1.5*-x),aes(label=paste(..rr.label..)), 
               label.x.npc = "right", label.y.npc = 0.9, parse=T, size=6, col="black", vjust=2, show.legend=F)+
  theme(legend.text = element_text(colour="black", size=17,face=1))+
  theme(legend.position = "bottom") +
  guides(color = guide_legend(nrow = 2, byrow = T))+
  scale_colour_manual(labels=c(col="Pot liming", col="Incubation liming", col="Incubation", col="Pot"),values = c("black", "black", "black", "black"))+
  guides(col = guide_legend(override.aes = list(linetype=c(0,0), shape = c(1, 2, 16, 17), color="black"),
                            nrow = 2))
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
p1
#> Warning: The dot-dot notation (`..rr.label..`) was deprecated in ggplot2 3.4.0.
#> ℹ Please use `after_stat(rr.label)` instead.
#> Warning: Removed 1 rows containing non-finite values (`stat_smooth()`).
#> Warning: Removed 1 rows containing non-finite values (`stat_poly_eq()`).
#> Removed 1 rows containing non-finite values (`stat_poly_eq()`).
#> Removed 1 rows containing non-finite values (`stat_poly_eq()`).
#> Warning: Duplicated aesthetics after name standardisation: colour
#> Duplicated aesthetics after name standardisation: colour
#> Duplicated aesthetics after name standardisation: colour
#> Duplicated aesthetics after name standardisation: colour
#> Duplicated aesthetics after name standardisation: colour
#> Duplicated aesthetics after name standardisation: colour
#> Warning: Removed 1 rows containing missing values (`geom_point()`).

创建于2023年1月30日,使用reprex v2.0.2

相关问题