5

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

Nova
  • 5,423
  • 2
  • 42
  • 62
  • 2
    Side note: please scrap rainbow colour scales: https://www.nature.com/articles/519291d – Tung Nov 19 '18 at 18:27
  • 2
    You can find better colormaps than rainbow in this thread https://stackoverflow.com/q/37482977/786542 – Tung Nov 19 '18 at 18:28
  • 1
    This is amazing. I use colorblind-friendly colors in all my publications... why would I do anything less on SO? Thanks for the reminder, Tung :) – Nova Nov 19 '18 at 21:16
  • that's great to hear. The world needs more thoughtful scientists like you :) – Tung Nov 19 '18 at 22:33

1 Answers1

4

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 2
    Yes! I just didn't know you could use geom_raster without spatial info. This is a real "face-palm" moment, but I really appreciate the help!! – Nova Nov 19 '18 at 21:15