Jelo A
Jelo A

Reputation: 39

Trying to replicate a visualisation in R

Relatively inexperienced R user. I am trying to create something similar to the visualisation below with data for another country. enter image description here

I've gone as far as creating the basic structure with data plotted in a vertical annual timeline with months running along the x axis but I have no idea how to edit the individual data points. I would appreciate any idea on how to move forward or even a completely different approach.

Here is my code using ggplot2:

p <- ggplot(forestfiresv, aes(y=year, x=dtstart))
p+geom_point() + 
  scale_x_datetime(lim=as.POSIXct(c("2021-01-01 00:01","2021-12-31 00:00", origin=lubridate::origin), "%m/%d %H:%M",tz="UTC"),expand = c(0,0), date_breaks="2 months", labels = date_format("%b"))+
  theme_bw()

A data sample:

structure(list(year = c("2000", "2000", "2000", "2000", "2000", 
"2000", "2000", "2000", "2000", "2000"), `Start date` = structure(c(11174, 11167, 11166, 11191, 
11222, 11144, 11151, 11192, 11244, 11187), class = "Date"), `Start time` = c("02:15", 
"16:05", "10:47", "15:41", "23:30", "15:29", "14:00", "13:53", 
"17:39", "11:09"), `End date` = structure(c(11174, 
11178, 11166, 11192, 11223, 11146, 11152, 11197, 11244, 11191
), class = "Date"), `End time` = c("14:00", "07:00", "19:00", 
"22:00", "02:00", "12:00", "00:10", "13:30", "19:07", "11:30"
), Δάση = c(200, 1400, 400, 0, 0, 0, 600, 2000, 0, 260), `Forest` = c(800, 
0, 0, 100, 100, 700, 0, 0, 0, 0), `Agricultural land` = c(0, 0, 0, 200, 0, 0, 200, 500, 0, 0), totalareaburnt = c(1000, 1400, 400, 500, 500, 700,    800, 2500, 350, 360), dtstart = structure(c(1628129700, 1627574700,    1627469220, 1629646860, 1632353400, 1625585340, 1626184800, 1629726780, 1634233140, 1629284940), class = c("POSIXct", "POSIXt"), tzone = "UTC"), dtend = structure(c(1628172000, 1628492400, 1627498800, 1629756000, 1632362400, 1625745600, 1626221400, 1630157400, 1634238420, 1629631800), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .internal.selfref = <pointer: (nil)>, row.names = c(NA, 10L), class = c("data.table", "data.frame"))

Upvotes: 1

Views: 85

Answers (1)

Elia
Elia

Reputation: 2584

This is the best I've obtained so far, but I bet it could be better. I've increased your example data frame because there was only one year of observation and I've injected some randomness to make the plot look better.

library(ggplot2)

ddf <- rbind(df,df,df,df,df,df,df,df,df,df)
ddf$year <- rep(2000:2009,each=10)
ddf$totalareaburnt <- sample(200:2500,100,replace = T)
ddf$dtstart <- ddf$dtstart+sample(86400*1:90,100,replace = T)
#duration in days
ddf$duration <- as.numeric(df$dtend-df$dtstart)/24
ddf$year <- as.integer(ddf$year)

ggplot(ddf,
       aes(y = year,
           x = dtstart)) +
  geom_point(aes(size = totalareaburnt,
                 col = duration), 
             shape = 17,
             alpha = 0.7) +
  scale_x_datetime(
    lim = as.POSIXct(
      c("2021-01-01 00:01", "2021-12-31 00:00", origin = lubridate::origin),
      "%m/%d %H:%M",
      tz = "UTC"
    ),
    expand = c(0, 0),
    date_breaks = "1 months",
    labels = scales::date_format("%b")
  ) +
  theme_minimal() +
  theme(
    legend.position = "top",
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line()
  ) +
  scale_y_continuous(trans = "reverse", breaks = unique(ddf$year))+
  scale_colour_gradientn(name= "Duartion (day)",colours = c( "yellow", "orange","darkred"))+
  scale_size_continuous(name="Area burned (ha)")

enter image description here

Upvotes: 1

Related Questions