如何在R中的刻面网格图上注释R-sq和p值?

如何在R中的刻面网格图上注释R-sq和p值?,r,ggplot2,regression,data-visualization,facet-grid,R,Ggplot2,Regression,Data Visualization,Facet Grid,我从互联网的不同部分提取代码,得到了这个图,它非常接近我想要的。然而,我对R中的编码非常陌生,我几乎不知道某些代码的真正含义,或者如何修复下面的一些美学 我的问题是: 如何将R-sq和p值放置在右上角?目前我使用的是stat\u cor,它只允许我根据y轴放置值 如何释放每个变量(ETRm、alpha和Ek)的y轴比例 谢谢你的指点 以下是我的数据和代码: params <- structure(list(month = structure(c(1L, 1L, 1L, 1L, 1L, 1

我从互联网的不同部分提取代码,得到了这个图,它非常接近我想要的。然而,我对R中的编码非常陌生,我几乎不知道某些代码的真正含义,或者如何修复下面的一些美学

我的问题是:

  • 如何将R-sq和p值放置在右上角?目前我使用的是
    stat\u cor
    ,它只允许我根据y轴放置值

  • 如何释放每个变量(ETRm、alpha和Ek)的y轴比例

  • 谢谢你的指点

    以下是我的数据和代码:

    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
    

    params要使用
    stat\u cor
    将R-sq和p值放置在右上角,您可以使用
    label.x.npc
    label.y.npc
    指示要放置文本的坐标(考虑到绘图中心为0.5)。您还可以使用
    vjust
    指定垂直对齐。 在您的情况下,可能是:

    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")
    

    谢谢您的帮助!这太完美了。如果其他人觉得这有帮助,请注意:您可以使用
    stat\u cor
    括号中的
    size=
    来更改批注的字体大小。