Search code examples
rggplot2colorbargeom-point

ggplot with geom_point doesn't show colorbar


I have managed a line-dot plot with ggplot2 where the dots display Intensity (y-axis) over Dates (x-axis), and also show the color of another value of interest.

However, I cannot manage to make it display the colorbar with the values. It si just never shown.

ggplot(
  DF, aes(x = mydate, y = myintensity)) +
  geom_point(size = 3, color = as.character(mycolormap[as.numeric(myhours)])) +
  geom_line(linetype = "dashed", color = "red") +
  geom_vline(xintercept = as.numeric(as.Date("2019-12-27")), color = "cyan", linetype = "solid")+ 
  geom_vline(xintercept = as.numeric(as.Date("2021-03-12")), color = "green", linetype = "solid")+ 
  scale_x_date(date_labels = "%b %Y", date_breaks = "1 month") +
  labs(x = "Date", y = "Intensity", title = "Chronology") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

I tried to add the "legend.position" in the theme argument, as I found in another question here

theme(axis.text.x = element_text(angle = 90, vjust = 0.5,
      legend.position = "top", legend.key.width = unit(2.5, "cm"))

or to use a "fill" argument in the AES argument, as also in another questions

ggplot(DF, aes(x = mydate, y = myintensity, 
       fill = as.character(mycolormap[as.numeric(myhours)]))

Nothing worked. How can I show the colorbar? Thanks!


dput(head(ExampleDF))
structure(list(Date = c("2019-05-26", "2019-05-27", "2019-09-19", 
"2019-09-20", "2019-12-25", "2019-12-25"), Time = c("11:11:00", 
"04:00:00", "07:00:00", "11:11:00", "04:00:00", "11:00:00"), 
    DateTime = c("2019-05-26 11:11:00", "2019-05-27 04:00:00", 
    "2019-09-19 07:00:00", "2019-09-20 11:11:00", "2019-12-25 04:00:00", 
    "2019-12-25 11:00:00"), TimeOnly = c("1905-06-21 11:11:00", 
    "1905-06-21 04:00:00", "1905-06-21 07:00:00", "1905-06-21 11:11:00", 
    "1905-06-21 04:00:00", "1905-06-21 11:00:00"), Intensity = c(5L, 
    10L, 10L, 10L, 7L, 10L), Comments = c("Feeling ill for days", 
    "", "", "", "", ""), SeriesComments = c("This section covers some ", 
    "", "", "", "", "")), row.names = c(NA, 6L), class = "data.frame")

library(lubridate)
library(viridis)

mydate <- as.Date(DF$DateTime,format = "%Y-%m-%d %H:%M:%S")
mytime <- lubridate::as_datetime(DF$DateTime)
myhours <- lubridate::hour(DF$DateTime)
myintensity <- as.numeric(DF$Intensity)
mycolormap<-magma(24) #creates a color for each hour

Solution

  • You need to map the color aesthetic to change the color of the points, not the fill aesthetic. If you want the magma colors, use scale_color_viridis_c in your plot.

    ggplot(ExampleDF, aes(as.Date(Date), Intensity, 
                          color = lubridate::hour(DateTime))) +
      geom_point(size = 3) +
      scale_color_viridis_c('Hour', option = 'magma') +
      geom_line(linetype = "dashed", color = "red") +
      geom_vline(xintercept = as.numeric(as.Date("2019-12-27")), color = "cyan", 
                 linetype = "solid")+ 
      geom_vline(xintercept = as.numeric(as.Date("2021-03-12")), color = "green", 
                 linetype = "solid")+ 
      scale_x_date(date_labels = "%b %Y", date_breaks = "1 month") +
      labs(x = "Date", y = "Intensity", title = "Chronology") +
      theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
    

    enter image description here