[英]Regression lines in ggplot
I have a 2 x 2 ancova with the resulting plot showing 4 regression lines and 4 groups in various colours on the plot.我有一个 2 x 2 ancova ,结果图显示了 4 条回归线和 4 组不同颜色的图。 I wish to retain the 4 colours in the plot but only show 2 regression lines for one of the variables - not all 4 as shown.
我希望保留图中的 4 种颜色,但仅显示其中一个变量的 2 条回归线- 并非如图所示的全部 4 条。 The data is here-
数据在这里——
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))
The plot is below-剧情如下——
ANCOVA Plot: ANCOVA 情节:
and the code i used to create it is here-我用来创建它的代码在这里-
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")
Would the easiest way to be to remove the lines all together and then use the predictvals()
function to redraw the required lines?最简单的方法是将所有线删除,然后使用
predictvals()
函数重新绘制所需的线吗? I would like only show the regression lines for the "Inshore" and "Offreef" locations while retaining the colours for all 4 sites.我只想显示“近海”和“离岸”位置的回归线,同时保留所有 4 个站点的颜色。
Note: This is my first question here so apologies if my question format is not correct or I haven't included all of the necessary information.注意:这是我在这里的第一个问题,如果我的问题格式不正确或我没有包含所有必要的信息,我深表歉意。 Thanks!
谢谢!
If I get you correct, you only specify the x and y inside the ggplot(aes(..)) call, then inside geom_smooth, you group according to the site (instead of Location?).如果我猜对了,您只在 ggplot(aes(..)) 调用中指定 x 和 y,然后在 geom_smooth 中,根据站点(而不是位置?)进行分组。 This will give you a prediction within the Site:
这将为您提供站点内的预测:
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.