如何在ggplot2中为图表数据的子集创建回归线?

时间:2017-12-08 08:46:47

标签: r ggplot2 regression

我有来自不同属的测量数据集。其中一些属具有多个数据点,另一些只有一个。我编写了以下代码,以生成包含measurementsMeasurement_A的{​​{1}}表格的散点图。

Measurement_B

尽管C和D只有一个数据点,但是图例显示了回归线和符号。我想找到一种方法来绘制具有多个数据点(属A和B)的那些属的回归线。我已经尝试了# Load ggplot2 and ggpmisc library(ggplot2) library(ggpmisc) measurements <- read.csv("measurements.csv") # Define colours and shapes for each genus (which I've called A, B, C and D) colour_specifications <- c("A"="green", "B"="red", "C"="yellow", "D"="blue") shape_specifications <- c("A"=15, "B"=16, "C"=17, "D"=18) # Create scatterplot of data from each genus # and plot a regression line for each genus and show the R^2 value for each line scatterplot <- ggplot(measurements, aes(x=Measurement_A, y=Measurement_B, colour=Genus, shape=Genus))+ + geom_point()+theme_classic()+ + scale_color_manual(values = colour_specifications)+ + scale_shape_manual(values= shape_specifications)+ + geom_smooth(method=lm, se=FALSE)+ + stat_poly_eq(formula = y ~ x, eq.with.lhs=FALSE, parse = TRUE) > scatterplot 函数,如下所示,

subset

但它对传说没有任何影响,这让我觉得我做错了什么。

有没有办法绘制A和B属的回归线而不是C和D?

ETA:

这是我用于此的数据:

scatterplot <- ggplot(measurements, aes(x=Measurement_A, y=Measurement_B,
    colour=Genus, shape=Genus))+
  geom_point()+theme_classic()+
  scale_color_manual(values = colour_specifications)+
  scale_shape_manual(values= shape_specifications)+
  geom_smooth(data=subset(measurements, Genus="A"),
          method='lm', se=FALSE)+
  geom_smooth(method=lm, se=FALSE)+
  stat_poly_eq(formula = y ~ x, eq.with.lhs=FALSE, parse = TRUE)

我想做的就是用一个图例创建我的数据的单个图,但是A和B属显示回归线和R ^ 2值。上面的两组代码给我一个带有回归线的图例,通过所有符号,而不仅仅是A和B.正如我上面解释的那样,我尝试了Genus <- c("A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "C", "D") Specimen <- c("Aa","Ab","Ac","Ad","Ae","Af","Ag","Ah","Ba", "Bb", "Bc", "Bd", "Ca", "Da") Measurement_A <- c(60, 80, 100, 105, 120, 130, 140, 95, 70, 80, 90, 100, 170, 55) Measurement_B <- c(10, 15, 30, 30, 35, 40, 40, 27, 10, 17, 20, 27, 15, 5) measurements <- data.frame(Genus, Specimen, Measurement_A, Measurement_B) 函数,但它没有改变图例。我试过的时候

subset

我收到一条错误消息,说scatterplot <- ggplot(measurements, aes(x=Measurement_A, y=Measurement_B, colour=Genus, shape=Genus))+ geom_point()+theme_classic()+ scale_color_manual(values = colour_specifications)+ scale_shape_manual(values= shape_specifications)+ geom_smooth(method=lm, se=FALSE)+ scale_color_manual(values = c("green","red"),limits = c("A","B"))+ stat_poly_eq(formula = y ~ x, eq.with.lhs=FALSE, parse = TRUE) scatterplot 然后我得到两个传说,一个带有预设符号,但所有属都是黑色,一个是颜色,但只有A和B的圆形符号。 / p>

Plot of data

ETA: 我找到了答案,我只需要在"Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale"括号中添加show.legend=FALSE

1 个答案:

答案 0 :(得分:0)

为什么不通过query.orderBy(cb.asc(EXPRESSION)) 指定两个因子级别?

subset