簡體   English   中英

如何在ggplot2 facet_grid中為R平方方程着色並將其移動到下一行?

[英]How to colour and move R squared equation to next line in ggplot2 facet_grid?

對於我需要的圖形尺寸,我希望 R 平方出現在下一行。 我還希望文本的顏色與因子z的顏色相對應

x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each  = 5)
df <- data.frame(x,y,z)

my.formula = y ~ x
ggplot(aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(formula = my.formula, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),  parse = TRUE, size = 2.5, col = "black")+
  facet_grid(.~z, scales = "free") + theme_classic()

在此處輸入圖片說明

這是一種方法。

  • 為了使顏色正確,注釋掉color = "black" ;
  • 要在回歸方程下方進行 r 平方,請使用atop ,請參閱?plotmath

atop的結果不是左對齊,而是在這里。

ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(
    formula = my.formula, 
    aes(label = paste("atop(", ..eq.label.., ",", ..rr.label.., ")")),
    label.y = 0.9,
    parse = TRUE, 
    size = 2.5
    #, col = "black"
  )+
  facet_grid(.~z, scales = "free") + 
  theme_classic()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM