简体   繁体   English

使用 ggplot2 创建散点图,其中许多回归线基于所有点和分组变量,并按分组变量点

[英]Create a scatter plot using ggplot2 where the many regression lines based on all points and grouping variable, and points by grouping variable

I'm interested in creating a scatter plot using ggplot2 in r where 1 regression line uses all of the points to create that line, the plot itself has points that are different from one another on the basis of a grouping variable with 2 level, and 2 regression lines associated with the grouping variables are present.我有兴趣在 r 中使用 ggplot2 创建散点图,其中 1 条回归线使用所有点来创建该线,该图本身具有基于具有 2 个级别的分组变量而彼此不同的点,并且存在 2 条与分组变量相关的回归线。

I want to combine the 1 overall regression lines from this graph:我想结合这张图中的 1 条整体回归线: 在此处输入图片说明

With the 2 grouping variable specific regression lines from this graph:使用该图中的 2 个分组变量特定回归线: 在此处输入图片说明

Is this possible?这可能吗? If so, how?如果是这样,如何?

Thanks in advance提前致谢


# 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 separated by Species with 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() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE) + labs(title="Scatter plot of Sepal.Length X Sepal.Width with dots as Species\n where Species is setosa or versicolor, with regression lines for each of the Species variable levels", 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 with regression line added for all data, and point differentiated by grouping variable
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") + scale_colour_manual(values = c("#ff0000","#0000ff"))
scatter_plot__sepal_length_x_sepal_width__points_is_species


Images from post:图片来自帖子:

Is this what you are trying to do?这是你想要做的吗?

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") + 
    geom_smooth(aes(col = Species), method=lm, se=FALSE, fullrange=TRUE) +
    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") + 
  scale_colour_manual(values = c("#ff0000","#0000ff"))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM