【问题标题】:Separate scatterplots and add line分离散点图并添加线
【发布时间】:2021-09-09 00:37:03
【问题描述】:

我有一个包含分组数据集的数据框,每个组有 12 行和 2 列,第三列是分类变量。 我正在尝试用拟合线绘制 3 个不同的散点图(作为分类变量的函数),但到目前为止,我只能在一个图中包含所有数据点。请帮忙:)

代码:

library(tidyverse)
library(dplyr)
library(readxl)
Data.tibble <- read_excel('')
Graph<-Data.tibble%>%
ggplot(aes(x=x, y=y))+
geom_point(aes(color=factor(Category)))

【问题讨论】:

  • ... + facet_wrap(~Category)
  • ... + geom_smooth(method = "lm", se = FALSE) 添加拟合线。

标签: r ggplot2 scatter-plot scatter3d


【解决方案1】:

library(tidyverse)
library(lubridate)

数据

df <-
  tibble(
    category = rep(1:3, each = 4),
    x = rep(seq(0,360,120), 3),
    #Random data 
    y = rnorm(12)
  ) %>% 
  #Transform category variable in a factor
  mutate(category = as.factor(category))

绘图代码

df %>% 
  # Defining aesthetics
  ggplot(aes(x,y,col = category))+
  geom_point()+
  # Facetting plot by category
  facet_grid(rows = vars(category))+
  # Fitting line
  geom_smooth()

绘制输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多