簡體   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

我有興趣在 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


圖片來自帖子:

這是你想要做的嗎?

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