0

I want a regression line for each group but none are appearing on my plot.

enter image description here

I want it to look something like this

enter image description here

(This is my first time posting on stack! If you need the long_data it is here: https://docs.google.com/spreadsheets/d/13SpwTeAs1UuWCJg-3XvYIs_2365HSvkBZjTMlA6sP-s/edit?usp=sharing)

g <- ggplot(long_data, aes(x=class, y=scores, col=group)) + 
  geom_jitter() +
  geom_point(aes(col = group)) +
  geom_smooth(method = "lm", formula = y ~ x, se  = FALSE) +
  scale_color_discrete(name = "School Year", labels = c("First Year", "Second Year", "Third Year")) +
  labs(title = "Psychology Scores by School Year", x = "Class", y = "Scores")
MrFlick
  • 195,160
  • 17
  • 277
  • 295
arkose8
  • 11
  • 1
  • 2
    add `aes(group = group)` inside `geom_smooth()`. – zephryl Mar 09 '22 at 01:13
  • 3
    also, using both `geom_jittter()` and `geom_point()` will plot each point twice, which likely isn't what you want. you only need `geom_jitter()`. – zephryl Mar 09 '22 at 01:18
  • Does this answer your question? [adding regression line per group with ggplot2](https://stackoverflow.com/questions/12281335/adding-regression-line-per-group-with-ggplot2) – jdobres Mar 09 '22 at 01:40

1 Answers1

0

Besides @zephryl suggestion, there is also the need of defining group as factor variable.

Sample code:

library(ggplot2)  

long_data$group<- factor(long_data$group, c(0, 1, 2))
levels(long_data$group) <- list( "First" = 0,  "Second" = 1 ,  "Third" = 2) # rename you factor levels

    (g <- ggplot(long_data, aes(x=class, y=scores, color=group)) + 
  geom_jitter() +
  #geom_point(aes(fill = group)) +
  geom_smooth(method = "lm", formula = y ~ x, se  = F, aes(group = group)) +
  scale_color_discrete(name = "School Year", labels = c("First Year", "Second Year", "Third Year")) +
  labs(title = "Psychology Scores by School Year", x = "Class", y = "Scores"))+
  theme_bw()

Plot:

enter image description here

Sample data:

long_data<-structure(list(...1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 
77, 78, 79, 80), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("First", 
"Second", "Third"), class = "factor"), social = c(12, 12, 14, 
14, 7, 7, 9, 9, 9, 9, 10, 10, 7, 7, 14, 14, 10, 10, 14, 14, 8, 
8, 8, 8, 8, 8, 13, 13, 7, 7, 5, 5, 5, 5, 7, 7, 6, 6, 12, 12, 
10, 10, 11, 11, 10, 10, 7, 7, 8, 8, 14, 14, 6, 6, 6, 6, 8, 8, 
8, 8, 10, 10, 10, 10, 11, 11, 9, 9, 7, 7, 8, 8, 9, 9, 8, 8, 12, 
12, 8, 8), develop = c(12, 12, 14, 14, 8, 8, 8, 8, 13, 13, 9, 
9, 9, 9, 15, 15, 10, 10, 14, 14, 9, 9, 10, 10, 8, 8, 10, 10, 
7, 7, 9, 9, 7, 7, 8, 8, 8, 8, 12, 12, 8, 8, 9, 9, 9, 9, 7, 7, 
9, 9, 13, 13, 8, 8, 9, 9, 5, 5, 8, 8, 8, 8, 14, 14, 12, 12, 12, 
12, 9, 9, 8, 8, 9, 9, 6, 6, 11, 11, 3, 3), person = c(11, 11, 
13, 13, 12, 12, 6, 6, 13, 13, 10, 10, 6, 6, 15, 15, 9, 9, 15, 
15, 7, 7, 12, 12, 8, 8, 11, 11, 7, 7, 7, 7, 7, 7, 6, 6, 10, 10, 
10, 10, 7, 7, 10, 10, 8, 8, 8, 8, 5, 5, 11, 11, 8, 8, 10, 10, 
9, 9, 12, 12, 9, 9, 10, 10, 10, 10, 9, 9, 7, 7, 4, 4, 5, 5, 7, 
7, 11, 11, 6, 6), class = c("exper", "stats", "exper", "stats", 
"exper", "stats", "exper", "stats", "exper", "stats", "exper", 
"stats", "exper", "stats", "exper", "stats", "exper", "stats", 
"exper", "stats", "exper", "stats", "exper", "stats", "exper", 
"stats", "exper", "stats", "exper", "stats", "exper", "stats", 
"exper", "stats", "exper", "stats", "exper", "stats", "exper", 
"stats", "exper", "stats", "exper", "stats", "exper", "stats", 
"exper", "stats", "exper", "stats", "exper", "stats", "exper", 
"stats", "exper", "stats", "exper", "stats", "exper", "stats", 
"exper", "stats", "exper", "stats", "exper", "stats", "exper", 
"stats", "exper", "stats", "exper", "stats", "exper", "stats", 
"exper", "stats", "exper", "stats", "exper", "stats"), scores = c(4, 
12, 10, 15, 6, 5, 6, 6, 4, 5, 7, 8, 3, 2, 8, 9, 6, 7, 3, 6, 5, 
8, 6, 10, 6, 6, 5, 11, 2, 4, 8, 7, 6, 6, 5, 8, 5, 10, 8, 10, 
4, 9, 5, 10, 4, 9, 7, 10, 4, 7, 7, 14, 6, 8, 5, 11, 10, 4, 7, 
13, 6, 8, 5, 12, 9, 11, 10, 9, 6, 9, 6, 14, 9, 12, 5, 14, 9, 
13, 4, 6)), spec = structure(list(cols = list(...1 = structure(list(), class = c("collector_double", 
"collector")), group = structure(list(), class = c("collector_double", 
"collector")), social = structure(list(), class = c("collector_double", 
"collector")), develop = structure(list(), class = c("collector_double", 
"collector")), person = structure(list(), class = c("collector_double", 
"collector")), class = structure(list(), class = c("collector_character", 
"collector")), scores = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x111c4268>, row.names = c(NA, 
-80L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
))
Rfanatic
  • 2,224
  • 1
  • 5
  • 21