Home > Enterprise >  can't find a solution for color the month in line graph in r
can't find a solution for color the month in line graph in r

Time:08-16

I would like to create a line graph with the following colors

yellow for january, february and march

red for april, may and june

green for july, august and september

blue for october, november and december

I have the following code:

temperature <- data.frame(
  stringsAsFactors = FALSE,
  Year= c(1998,1998,1998,1998,1998,1998,1998,1998,1998,1998,1998,1998,1999,1999,1999,1999,1999,1999,1999,1999,1999,1999,1999,1999,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2000,2001,2001,2001,2001,2001,2001,2001,2001,2001,2001,2001,2001,2002,2002,2002,2002,2002,2002,2002,2002,2002,2002,2002,2002),
  Month = c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12),
  Temperature = c(5,6,7,12,15,16,27,28,22,10,11,6,6,6,7,13,16,15,25,26,24,12,11,4,5,6,7,12,15,17,28,30,22,10,11,7,7,7,7,12,18,17,25,24,20,3,3,4,5,6,7,12,15,16,27,28,22,10,11,6),
  check.names = FALSE
)

temperature$date = as.Date(paste(temperature$Year, temperature$Month,1), format = "%Y %m %d")

ggplot(temperature,
       aes(
         x = date,
         y = Temperature
        ))  
  geom_line()  
  ggtitle("Time series")  
scale_x_date(date_breaks = "year", date_labels = "%Y", date_minor_breaks = "month")  
  xlab("Year")  
  ylab("Temperature")

But I don't know how to color the month and create a legend next to the line graph. Could someone please help?

The line is subdivided in different colors based on months

CodePudding user response:

Alternatively, to allow control of the colours with standard scale_colour_xxxx calls, you can override the default grouping defined by colour=Month with group=1. This will allow use of a legend.

ggplot(temperature,
       aes(
         x = date,
         y = Temperature,
         colour=as.factor(Month),
         group=1
       ))  
  geom_line()  
  ggtitle("Time series")  
  scale_x_date(
    date_breaks = "year", 
    date_labels = "%Y", 
    date_minor_breaks = "month"
  )  
  xlab("Year")  
  ylab("Temperature")

enter image description here

For a continuous "flow" of colours across Month, use colour=Month.

enter image description here

CodePudding user response:

I'm not sure how elegant (especially the part regarding ggplot) my approach is, but I used seas::mkseas() in the past to determine the season given in datetime objects. In combination with color initialization, one way to solve this might look like this:

library(ggplot2)
library(seas)

temperature$season <- mkseas(temperature$date, width="JFM")

colors <- c("JFM" = "yellow",
            "AMJ" = "red",
            "JAS" = "green",
            "OND" = "blue")

ggplot(temperature,
       aes(
         x = date,
         y = Temperature
       ))  
  geom_line(col = colors[temperature$season])  
  ggtitle("Time series")  
  scale_x_date(date_breaks = "year", date_labels = "%Y", date_minor_breaks = "month")  
  xlab("Year")  
  ylab("Temperature")

  •  Tags:  
  • r
  • Related