[英]How to annotate R-sq and p-value on facet_grid plots in R?
I was extracting codes from different parts of the internet and came up with this graph which is pretty close to what I wanted.我从互联网的不同部分提取代码,并提出了这个非常接近我想要的图表。 However, I am quite new to coding in R and I have very little clue what some of the codes really mean, or how to fix a few of the aesthetics below.
但是,我对 R 中的编码非常陌生,而且我几乎不知道某些代码的真正含义,或者如何解决下面的一些美学问题。
My questions are:我的问题是:
How can I place the R-sq and p-values in the top right corner?如何将 R-sq 和 p 值放在右上角? Currently I am using
stat_cor
which only allows me to place the values according to the y axis.目前我正在使用
stat_cor
,它只允许我根据 y 轴放置值。
How can I free the scale of the y axis for each variable, hence, ETRm, alpha, and Ek?如何释放每个变量的 y 轴刻度,即 ETRm、alpha 和 Ek?
Thank for any pointers.感谢您的任何指点。
Here are my data and codes:这是我的数据和代码:
params <- structure(list(month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
), class = c("ordered", "factor")), site = c("Bluff", "Palm",
"Port", "Bluff", "Palm", "Port", "Bluff", "Palm", "Port", "Bluff",
"Palm", "Port", "Bluff", "Palm", "Port", "Bluff", "Palm", "Port",
"Bluff", "Palm", "Port", "Bluff", "Palm", "Port", "Bluff", "Palm",
"Port", "Bluff", "Palm", "Port", "Bluff", "Palm", "Port", "Bluff",
"Palm", "Port", "Bluff", "Palm", "Port", "Bluff", "Palm", "Port",
"Bluff", "Palm", "Port", "Bluff", "Palm", "Port", "Bluff", "Palm",
"Port", "Bluff", "Palm", "Port", "Bluff", "Palm", "Port", "Bluff",
"Palm", "Port", "Bluff", "Palm", "Port"), variable = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("max",
"slope", "ek"), class = "factor"), mean = c(18.4112684600317,
12.0927190160653, 17.0039206367045, 0.194928481301732, 0.242361464222705,
0.17244796468276, 95.9113769030907, 50.218551634525, 102.379921755477,
16.1452458726515, 15.78921901878, 15.2843934219522, 0.216428983074687,
0.197814168838787, 0.238727179767021, 76.1007134423614, 83.2388922446894,
64.6765873418609, 8.88529718419458, 20.5318236818331, 16.9028346359378,
0.205327118763673, 0.224583425151928, 0.250641933521497, 43.0448523961101,
92.5904725154284, 68.1731338482127, 13.4688404508323, 10.7385305579172,
16.5826378988362, 0.252594073275179, 0.199623406268575, 0.238485175752726,
52.823744224331, 53.0944916519813, 70.0963144948727, 25.0436867551474,
23.0217111904282, 24.3625460643338, 0.200083982943045, 0.202815121141515,
0.177720769745539, 127.391986068071, 115.78757627439, 154.919609106235,
30.6918280467953, 27.6741791477911, 30.5789999055665, 0.193556546349104,
0.164808924194275, 0.171605947547358, 163.804020720028, 177.161548018174,
193.016869884844, 28.5624864437648, 34.8426077334045, 24.9797351428063,
0.193556546349104, 0.164808924194275, 0.171605947547358, 150.89246028789,
220.530120347006, 158.671317962566), sd = c(1.48992847472219,
3.09240083289092, 1.62063033926763, 0.0229764010207009, 0.0280923627027394,
0.030729204333678, 15.3201126879475, 13.7030243680367, 25.2134299805608,
3.37226297719465, 1.57360530354448, 2.15560392775395, 0.0191629833440665,
0.0382634121304685, 0.014457194305487, 20.7248039246539, 20.6426872975474,
12.4899935218917, 2.6269303020975, 4.73640350857756, 2.94826573904166,
0.0558874457337392, 0.0189817499092785, 0.0179818534693642, 4.02530794719126,
24.1662604001592, 15.7142496249819, 3.7083082283979, 2.85099637043216,
2.91322999227116, 0.0229270991259808, 0.0246956175791608, 0.0198928041049747,
11.6129210272195, 7.65309211133912, 14.0714947137942, 5.58981966083196,
6.31626426233187, 5.38173464809157, 0.0181636366470283, 0.0326870026454955,
0.0518428334177721, 37.5936261061099, 33.4578841104308, 88.9197348969026,
7.86641344325536, 8.60007250893753, 6.46749309586222, 0.0355704165245184,
0.036827848988506, 0.0527944645552859, 57.2326154870389, 79.7755580837637,
65.2525890645308, 4.40174523479545, 6.34673267541054, 4.58135872263371,
0.0355704165245184, 0.036827848988506, 0.0527944645552859, 30.5837011400726,
54.832387442635, 59.1894331597497), n = c(10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L), se = c(0.471156753086271,
0.977903007013706, 0.512488311725713, 0.00726577596588324, 0.008883585099622,
0.00971742763791392, 4.84464501043585, 4.33327678357854, 7.97318663637472,
1.06640318769955, 0.497617689732119, 0.681661814490754, 0.00605986741311204,
0.0120999533381996, 0.00457176625809549, 6.5537584462303, 6.52779088868756,
3.94968274899259, 0.83070830091423, 1.4977823004718, 0.932323488281127,
0.0176731621127676, 0.00600255636890144, 0.00568636135145918,
1.27291413967012, 7.64204253932352, 4.96928205353896, 1.17267002676813,
0.901564213143874, 0.921244212349158, 0.00725018533785552, 0.00780943997746417,
0.00629065700192673, 3.67232807336759, 2.42012022148986, 4.4497973378609,
1.76765618378189, 1.9973781372492, 1.70185392506141, 0.00574384623963131,
0.0103365378243715, 0.0163941433956852, 11.8881484000074, 10.5803119478918,
28.118889121257, 2.48757834972546, 2.71958171708414, 2.04520089343383,
0.0112483533538369, 0.0116459884128407, 0.0166950755843732, 18.098542138767,
25.2272465155751, 20.6346804666913, 1.39195406217463, 2.00701309545114,
1.44875283418021, 0.0112483533538369, 0.0116459884128407, 0.0166950755843732,
9.67141548805177, 17.3395233863542, 18.7173422199106)), row.names = c(1L,
2L, 3L, 22L, 23L, 24L, 43L, 44L, 45L, 4L, 5L, 6L, 25L, 26L, 27L,
46L, 47L, 48L, 7L, 8L, 9L, 28L, 29L, 30L, 49L, 50L, 51L, 10L,
11L, 12L, 31L, 32L, 33L, 52L, 53L, 54L, 13L, 14L, 15L, 34L, 35L,
36L, 55L, 56L, 57L, 16L, 17L, 18L, 37L, 38L, 39L, 58L, 59L, 60L,
19L, 20L, 21L, 40L, 41L, 42L, 61L, 62L, 63L), class = "data.frame")
library(ggplot2)
library(ggpubr)
plot <- ggplot(params, aes(month, mean, color = site, group = site)) +
geom_point() +
stat_smooth(method=lm, se=FALSE) +
geom_errorbar(aes(ymin = mean - se, ymax = mean +se), width = 0.15) +
facet_grid(rows = vars(variable), cols = vars(site),
switch = "y", scale = "free_y",
labeller = labeller(variable = params.labs, site = site.labs)) +
stat_cor(label.y = 4.4) +
theme_bw() +
theme(plot.background = element_blank(),
strip.background = element_blank(),
strip.placement = "outside",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(size=1, colour = "black"),
panel.spacing = unit(0.3, "lines"),
axis.line = element_line(size=0.1, colour = "black"),
axis.ticks.y = element_line(size=0.5, colour = "black"),
axis.text = element_text(size=10, color="black", margin = margin(t = 0.5, r = 0.5)),
text = element_text(size = 18),
legend.position="none",
plot.margin = unit(c(1,1,1,1), "cm")) +
ylab(NULL) +
xlab("Month 2019")
plot
To place the R-sq and p-values in the top right corner using stat_cor
you can indicate the coordinates where you want to place the text using label.x.npc
and label.y.npc
(considering the centre of your plot as 0.5). To place the R-sq and p-values in the top right corner using
stat_cor
you can indicate the coordinates where you want to place the text using label.x.npc
and label.y.npc
(considering the centre of your plot as 0.5 )。 You can also use vjust
to specify vertical justification.您还可以使用
vjust
指定垂直对齐方式。 In your case it could be:在您的情况下,它可能是:
params %>%
ggplot(aes(x = month,
y = mean,
color = site,
group = site)) +
geom_point() +
stat_smooth(method=lm,
se=FALSE) +
geom_errorbar(aes(ymin = mean - se,
ymax = mean +se),
width = 0.15) +
facet_grid(rows = vars(variable),
cols = vars(site),
switch = "y",
scale = "free_y") +
stat_cor(label.x.npc = .65,
label.y.npc = 1.0,
vjust = 1) +
theme_bw() +
theme(plot.background = element_blank(),
strip.background = element_blank(),
strip.placement = "outside",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(size=1, colour = "black"),
panel.spacing = unit(0.3, "lines"),
axis.line = element_line(size=0.1, colour = "black"),
axis.ticks.y = element_line(size=0.5, colour = "black"),
axis.text = element_text(size=10, color="black", margin = margin(t = 0.5, r = 0.5)),
text = element_text(size = 18),
legend.position="none",
plot.margin = unit(c(1,1,1,1), "cm")) +
ylab(NULL) +
xlab("Month 2019")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.