Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raster-like timeseries graph in ggplot2

Tags:

I'm trying to recreate a graph like the one here using ggplot2.

enter image description here

I can get pretty close if I mess around with the size and shape of points using coord_equal, but...

Example data and code
library(ggplot2)
df <- data.frame()
Years <- 1990:2020
for(i in 1:length(Years)) {
  Year <- Years[i]
  week <-1:52
  value <- sort(round(rnorm(52, 50, 30), 0))
  df.small <- data.frame(Year = Year, week = week, value = value)
  df <- bind_rows(df, df.small)
}


ggplot(df, aes(week, Year, color = value)) +
  geom_point(shape = 15, size = 2.7) +
  scale_color_gradientn(colours = rainbow(10)) +
  coord_equal()

enter image description here

The problem is,

with my real data I want to "stretch" the graph so I can see it more clearly (my timeseries is shorter) and when I don't use coord_equal, squares don't fill the graphing area:

ggplot(df, aes(week, Year, color = value)) +
  geom_point(shape = 15, size = 2.7) +
  scale_color_gradientn(colours = rainbow(10))

enter image description here

like image 718
Nova Avatar asked Nov 19 '18 18:11

Nova


1 Answers

Is this as simple as using the geom_raster geom?

ggplot(df, aes(week, Year)) +
  geom_raster(aes(fill = value)) +
  scale_fill_gradientn(colours = rainbow(10)) +
  coord_equal()

enter image description here

like image 95
Rui Barradas Avatar answered Oct 11 '22 17:10

Rui Barradas