简体   繁体   中英

geom_line showing straight lines instead of smooth lines in R

I am trying to plot a timeseries line plot summarized by month in R via ggplot . The sample file can be downloaded from here . The plot has been grouped by Pixel IDs . I was expecting smooth lines, but instead the plot shows straight lines.

Are these straight lines because of how the data is, or should I be summarizing the values another way?

    library(dplyr)
    library(tidyverse)
    library(lubridate)
    
    df = read.csv("/df.csv")
    
   # Use lubridate to make a date column
   df $Date = as.Date(df$T, format = "%m/%d/%Y")
 
   # Now drop the original Date (T) column
   df = dplyr::select(df , -c('T'))

   # Plot

    df%>% pivot_longer(-c(GRID_CODE, Date)) %>%
      mutate(Pixel = paste0(GRID_CODE, ".", name),
             Date = month(Date, label = TRUE, abbr = FALSE)) %>% 
      ggplot(aes(x = Date, y = value, group = Pixel, color = Pixel)) +
      geom_line() +
      labs(x = "Month")

Current plot

在此处输入图片说明

I was able find the error. It was being caused because when I mutated it with months, it was creating ties in the plot. So removing that fixed the problem.

library(dplyr)
library(tidyverse)
library(lubridate)

df = read.csv("/df.csv")
    
   # Use lubridate to make a date column
   df $Date = as.Date(df$T, format = "%m/%d/%Y")
 
   # Now drop the original Date (T) column
   df = dplyr::select(df , -c('T'))

df %>% pivot_longer(-c(GRID_CODE, Date)) %>%
  mutate(Pixel = paste0(GRID_CODE, ".", name)) %>% 
  ggplot(aes(x = Date, y = value, group = Pixel, color = Pixel)) +
  geom_line() +
  #geom_smooth(method = "loess", se = F) +
  labs(x = "Month")

Plot

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM