0

I have a data frame with grouped datasets, each group has 12 rows and 2 columns with the third column being a categorical variable. I am trying to plot 3 different scatterplots (as a function of the categorical variable) with fitted lines but so far I'm only able to have all data points in one plot. Please help :)

Data.tibble

Code:

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)))
David N.F.
  • 35
  • 4

1 Answers1

0

Libraries

library(tidyverse)
library(lubridate)

Data

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

Plot code

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

Plot output

enter image description here

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32