簡體   English   中英

ggplot 中的回歸線

[英]Regression lines in ggplot

我有一個 2 x 2 ,結果圖顯示了 4 條回歸線和 4 組不同顏色的圖。 我希望保留圖中的 4 種顏色,但僅顯示其中一個變量的 2 條- 並非如圖所示的全部 4 條。 數據在這里——

PLD.df <- structure(list(Site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Inshore", "OffReef"
), class = "factor"), Depth = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Deep", "Shallow"
), class = "factor"), PLD = c(37L, 38L, 47L, 51L, 51L, 53L, 34L, 
39L, 40L, 45L, 49L, 49L, 26L, 29L, 35L, 35L, 36L, 36L, 37L, 38L, 
38L, 40L, 41L, 46L, 47L, 52L, 37L, 38L, 40L, 45L, 45L), Location = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("ID", 
"IS", "OD", "OS"), class = "factor"), b = c(0.052, 0.05, 0.039, 
0.043, 0.036, 0.033, 0.055, 0.051, 0.048, 0.046, 0.041, 0.04, 
0.05, 0.05, 0.051, 0.049, 0.056, 0.052, 0.047, 0.045, 0.047, 
0.045, 0.045, 0.045, 0.039, 0.038, 0.046, 0.049, 0.046, 0.044, 
0.041)), .Names = c("Site", "Depth", "PLD", "Location", "b"), class = "data.frame", row.names = c(NA, 
-31L))

劇情如下——

ANCOVA 情節:

在此處輸入圖片說明

我用來創建它的代碼在這里-

ggplot(PLD.df, aes(x=PLD, y=b, colour=Location)) + 
  geom_point(aes(shape=Location),size=3) + 
  scale_shape(solid=FALSE) + 
  scale_colour_manual(values=cb_palette) + 
  geom_smooth(aes(linetype=Location),method=lm, se=FALSE, fullrange=F) + 
  theme(panel.border=element_rect(colour="black", fill=NA,size=3),
        panel.background=element_rect(fill=FALSE),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank()) + 
  theme(legend.position="NONE")

最簡單的方法是將所有線刪除,然后使用predictvals()函數重新繪制所需的線嗎? 我只想顯示“近海”和“離岸”位置的回歸線,同時保留所有 4 個站點的顏色。

注意:這是我在這里的第一個問題,如果我的問題格式不正確或我沒有包含所有必要的信息,我深表歉意。 謝謝!

如果我猜對了,您只在 ggplot(aes(..)) 調用中指定 x 和 y,然后在 geom_smooth 中,根據站點(而不是位置?)進行分組。 這將為您提供站點內的預測:

cb_palette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

ggplot(PLD.df, aes(x=PLD, y=b)) + 
  geom_point(aes(shape=Location,colour=Location),size=3) + 
  scale_shape(solid=FALSE) + 
  scale_colour_manual(values=cb_palette) + 
  geom_smooth(aes(linetype=Site),
              method=lm, se=FALSE, fullrange=F,col="gray") + 
  theme(panel.border=element_rect(colour="black", fill=NA,size=3),
        panel.background=element_rect(fill=FALSE),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank()) + 
  theme(legend.position="NONE")

在此處輸入圖片說明

暫無
暫無

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

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