Home > Mobile >  Why do ggline and geom_line have different outputs?
Why do ggline and geom_line have different outputs?

Time:10-11

Data

Here is the dput of my data:

slack.work <- structure(list(Mins_Work = c(435L, 350L, 145L, 135L, 15L, 60L, 
60L, 390L, 395L, 395L, 315L, 80L, 580L, 175L, 545L, 230L, 435L, 
370L, 255L, 515L, 330L, 65L, 115L, 550L, 420L, 45L, 266L, 196L, 
198L, 220L, 17L, 382L, 0L, 180L, 343L, 207L, 263L, 332L, 0L, 
0L, 259L, 417L, 282L, 685L, 517L, 111L, 64L, 466L, 499L, 460L, 
269L, 300L, 427L, 301L, 436L, 342L, 229L, 379L, 102L, 146L, NA, 
94L, 345L, 73L, 204L, 512L, 113L, 135L, 458L, 493L, 552L, 108L, 
335L, 395L, 508L, 546L, 396L, 159L, 325L, 747L, 650L, 377L, 461L, 
669L, 186L, 220L, 410L, 708L, 409L, 515L, 413L, 166L, 451L, 660L, 
177L, 192L, 191L, 461L, 637L, 297L), Consec_Month = c("1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", 
"4", "4", "4", "4", "4", "4", "4")), row.names = c(NA, -100L), class = c("tbl_df", 
"tbl", "data.frame"))

Problem

I discovered an oddity between geom_line and ggline and I'm not sure why there is a difference or how to fix it in the geom_line version. As an example, here is the ggline plot from the ggpubr package:

library(ggplot2)
library(ggpubr)


slack.work %>% 
  group_by(Consec_Month) %>% 
  summarise(Mean_Work = mean(Mins_Work,
                             na.rm=T)) %>% 
  mutate(Consec_Month = factor(Consec_Month,
                                  levels = c(1:14))) %>% 
  arrange(Consec_Month) %>% 
  ggline(x="Consec_Month",
         y="Mean_Work")

Which seems normal:

enter image description here

However if I do the same thing with the ggplot2 version:

slack.work %>% 
  group_by(Consec_Month) %>% 
  summarise(Mean_Work = mean(Mins_Work,
                             na.rm=T)) %>% 
  mutate(Consec_Month = factor(Consec_Month,
                               levels = c(1:14))) %>% 
  arrange(Consec_Month) %>% 
  ggplot(aes(x=Consec_Month,
             y=Mean_Work)) 
  geom_point() 
  geom_line()

I get a warning that each group only has one observation and this incomplete plot:

enter image description here

Is there a reason for this and how do I fix it? I prefer the ggplot2 version when possible because its more customizable.

CodePudding user response:

This is because you have made your x-axis a factor rather than continuous. If you tell ggplot2 that it's continuous in the geom_line() call it will know to connect the points:

slack.work %>% 
  group_by(Consec_Month) %>% 
  summarise(Mean_Work = mean(Mins_Work,
                             na.rm=T)) %>% 
  mutate(Consec_Month = factor(Consec_Month,
                               levels = c(1:14))) %>% 
  arrange(Consec_Month) %>% 
  ungroup()  %>% 
  ggplot(aes(x=Consec_Month,
             y=Mean_Work)) 
  geom_point() 
  geom_line(aes(x=as.numeric(Consec_Month)))

enter image description here

  • Related