在r中使用ggplot2创建散点图,其中1条回归线与除点以外的所有点通过分组变量进行区分

在r中使用ggplot2创建散点图,其中1条回归线与除点以外的所有点通过分组变量进行区分,r,ggplot2,plot,graph,R,Ggplot2,Plot,Graph,我感兴趣的是在r中使用ggplot2创建散点图,其中1回归线使用所有点创建该线,但该图本身具有基于2级分组变量的不同点 我希望回归线如下图所示: 我想在这个图中区分点: 这可能吗 谢谢 下面是我用来绘制图的代码: # creates data for scatter plot ## dataset of interest iris ## for iris colnames(iris) ### creates dataset with just cases where iris$Spec

我感兴趣的是在r中使用ggplot2创建散点图,其中1回归线使用所有点创建该线,但该图本身具有基于2级分组变量的不同点

我希望回归线如下图所示:

我想在这个图中区分点:

这可能吗

谢谢

下面是我用来绘制图的代码:

# creates data for scatter plot

## dataset of interest
iris

## for iris
colnames(iris)

### creates dataset with just cases where iris$Species == setosa or versicolor

#### unique values for iris$Species
unique(iris$Species)

#### loads tidyverse package
library(tidyverse)

##### filters dataset with just cases where iris$Species == setosa or versicolor
iris__setosa_or_versicolor <- iris %>% filter(iris$Species != "virginica")

##### turns iris__setosa_or_versicolor to dataframe
iris__setosa_or_versicolor <- data.frame(iris__setosa_or_versicolor)

##### unique values for iris__setosa_or_versicolor$Species
unique(iris__setosa_or_versicolor$Species)

## creates scatter plot

### loads ggplot2
library(ggplot2)

### Basic scatter plot
scatter_plot__sepal_length_x_sepal_width__points_is_species <- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point()
scatter_plot__sepal_length_x_sepal_width__points_is_species

### Basic scatter plot with regression line added
scatter_plot__sepal_length_x_sepal_width__points_is_species <- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point() + geom_smooth(method=lm, se=FALSE, color="green") + labs(title="Scatter plot of Sepal.Length X Sepal.Width with dots as Species where\n Species is setosa or versicolor but not differentiated by species", x="Sepal.Length", y = "Sepal.Width")
scatter_plot__sepal_length_x_sepal_width__points_is_species

### Basic scatter plot separated by Species
scatter_plot__sepal_length_x_sepal_width__points_is_species <- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE) + labs(title="Scatter plot of Sepal.Length X Sepal.Width with dots as\n Species where Species is setosa or versicolor", x="Sepal.Length", y = "Sepal.Width") + scale_colour_manual(values = c("#ff0000","#0000ff"))
scatter_plot__sepal_length_x_sepal_width__points_is_species

### Basic scatter plot separated by Species with no regression lines
scatter_plot__sepal_length_x_sepal_width__points_is_species <- ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + geom_point() + labs(title="Scatter plot of Sepal.Length X Sepal.Width with dots as Species\n where Species is setosa or versicolor, with no regression lines", x="Sepal.Length", y = "Sepal.Width") + scale_colour_manual(values = c("#ff0000","#0000ff"))
scatter_plot__sepal_length_x_sepal_width__points_is_species

#为散点图创建数据
##感兴趣的数据集
虹膜
##给艾里斯
colnames(iris)
###仅在iris$Species==setosa或versicolor的情况下创建数据集
####iris$物种的唯一值
独特(鸢尾属$种)
####加载tidyverse包
图书馆(tidyverse)
#####仅在iris$Species==setosa或versicolor的情况下过滤数据集
iris_uuusetosa_或_versicolor%过滤器(iris$物种!=“virginica”)
#####将iris_uusetosa_或花色转换为数据帧

iris_uuusetosa_或versicolor你想要这样的东西吗

### Basic scatter plot with regression line added
scatter_plot__sepal_length_x_sepal_width__points_is_species <-ggplot(iris__setosa_or_versicolor, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point(aes(col=Species)) + geom_smooth(method=lm, se=FALSE, color="green") + labs(title="Scatter plot of Sepal.Length X Sepal.Width with dots as Species where\n Species is setosa or versicolor but not differentiated by species", x="Sepal.Length", y = "Sepal.Width")
scatter_plot__sepal_length_x_sepal_width__points_is_species
添加回归线的基本散点图
散点图、萼片长度、萼片宽度、点是物种是。这太棒了@梅尔:很高兴知道。如果答案对你有用,你能把它标为已回答吗?谢谢