【问题标题】:Create a scatter plot using ggplot2 where the many regression lines based on all points and grouping variable, and points by grouping variable使用 ggplot2 创建散点图,其中许多回归线基于所有点和分组变量,以及通过分组变量的点
【发布时间】:2020-10-01 02:53:05
【问题描述】:

我有兴趣在 r 中使用 ggplot2 创建一个散点图,其中 1 条回归线使用所有点来创建该线,该图本身具有基于具有 2 的分组变量而彼此不同的点水平,并且存在与分组变量相关的 2 条回归线。

我想结合这张图中的 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 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


来自帖子的图片:

【问题讨论】:

    标签: r ggplot2 plot graph


    【解决方案1】:

    这是你想要做的吗?

    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"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2018-02-13
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多