0

I have traffic density (number of cars) data of various segments of a road. I want to plot a contour map like in the attached figure in R.

data <- structure(list(timestamp = structure(c(1598950800, 1598950800, 
1598950800, 1598951100, 1598951100, 1598951100, 1598951400, 1598951400, 
1598951400, 1598951700, 1598951700, 1598951700, 1598952000, 1598952000, 
1598952000, 1598952300, 1598952300, 1598952300, 1598952600, 1598952600, 
1598952600, 1598952900, 1598952900, 1598952900, 1598953200, 1598953200, 
1598953200, 1598953500, 1598953500, 1598953500, 1598953800, 1598953800, 
1598953800, 1598954100, 1598954100, 1598954100, 1598954400, 1598954400, 
1598954400), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    Road_segment = c("L1", "L2", "L3", "L1", 
    "L2", "L3", "L1", "L3", "L2", 
    "L2", "L3", "L1", "L1", "L3", 
    "L2", "L2", "L3", "L1", "L1", 
    "L3", "L2", "L1", "L2", "L3", 
    "L3", "L2", "L1", "L2", "L3", 
    "L1", "L1", "L2", "L3", "L1", 
    "L2", "L3", "L3", "L2", "L1"
    ), length_mi = c(2, 1.5, 1, 
    2, 1.5, 1, 
    2, 1, 1.5, 
    1.5, 1, 2, 
    2, 1, 1.5, 
    1.5, 1, 2, 
    2, 1, 1.5, 
    2, 1.5, 1, 
    1, 1.5, 2, 
    1.5, 1, 2, 
    2, 1.5, 1, 
    2, 1.5, 1, 
    1, 1.5, 2), 
    avg_tdensity = c(59.9809611866698, 56.3123856677006, 57.6719458363159, 
    59.3508907977412, 56.4167760279965, 57.3786586335799, 56.8455221506403, 
    53.8045315358307, 54.8844746679392, 54.9068440308598, 52.5369343036666, 
    54.0965759961823, 53.7734629762189, 53.8517557464408, 53.941233198123, 
    57.5837111270182, 54.4867971049073, 59.4453392189613, 61.6561779209417, 
    55.8401435616003, 60.4730871709218, 63.5737294201861, 66.6594587608367, 
    56.9064165274795, 58.3753380259286, 65.83552055993, 63.5973415254911, 
    55.3480175773483, 56.538564781675, 54.6707229778096, 58.3641533444683, 
    52.1665970730931, 56.8069971367215, 60.7017517696652, 53.6516742225404, 
    56.3745227869244, 53.4913604549431, 52.4971665473634, 56.1781694901774
    )), row.names = c(NA, -39L), class = c("tbl_df", "tbl", "data.frame"
))

Please help me to plot.

Road segments Traffic Density contour plot

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
usama
  • 15
  • 6
  • It will be extremely challenging to answer your question without at least a sample of your data. Please [edit] your question, copy the output of the `dput(data)` command from R and paste it into your question (preferred). Alternatively paste the tab delimited data from a spreadsheet program. Please see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more information. – Ian Campbell Jun 10 '21 at 18:11
  • I have added data – usama Jun 10 '21 at 19:22

1 Answers1

1

It seems that you're looking for a tile plot. Here is an approach with ggplot2:

library(ggplot2)
ggplot(data, aes(x = Road_segment, y = timestamp, fill = avg_speed_mph)) +
  geom_tile() + 
  labs(y = "Time", x = "Road Segment", fill = "Average Speed")

enter image description here


I couldn't get variable width tiles to work with geom_tile, so here's a geom_bar approach:

ggplot(data, aes(x = timestamp, y = length_mi,
                 group = Road_segment, fill = avg_tdensity)) +
    geom_bar(stat = 'identity', position = position_stack(), width = 5*60) +
    labs(y = "Distance (mi)", x = "Time", fill = "Traffic Density") +
    coord_flip() +
    scale_fill_gradient(low = "yellow", high = "red", na.value = NA)

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57