-1

Using R, I want to plot a map of several observations (where an observation is a particular vehicle given by its registration plate) of speeding over defined distances along several different roads. I also want to color code the observations according to how much above the speed limit a vehicle traveled (red- +30mph, yellow- 20-30mph, blue- 0-20mph).

INPUT

  1. For each vehicle:
    • the road on which it was found speeding
    • the locations (start and end) for the given road along which it was speeding (can be multiple times on one road)
    • the category of speeding (red, yellow or blue as above).
  2. For each road
    • the distance of the road.

Data

$Vehicle1
Road    Start     End
1 157398137 166811234
1 216984017 238298694
1 238298694 247249719
2       0   8794530
2   8794530  26703134
2  59852594  73085123
2 206737339 218577503
2 218695178 231142027
3  96301465 115456078
3 116345621 126898764


$Vehicle2
Road    Start     End
       1 157398137 166811234
1 216984017 238298694
1 238298694 247249719
2     0   8794530
2   8794530  26703134
2  59852594  73085123
2 206737339 218577503
3   3469683  56797911
3  96301465 115456078
3 116345621 126898764


$Vehicle3
Road    Start     End
1 157398137 166811234
1 216984017 238298694
1 238298694 247249719
2       0   8794530
2   8794530  26703134
2  59852594  73085123
2 206737339 218577503
2 218695178 231142027
2 231142027 241946296
3   3469683  56797911
3  96301465 115456078
3 116345621 126898764


Road    Length
1   529290651
2   249139323
3   298024420

EXPECTED OUTPUT

enter image description here

Kaleb
  • 1,022
  • 1
  • 15
  • 26
  • This looks like a job that ggplot2 qith `geom_segment` could handle but without data this is tough to help. Have a look at this answer. The color can be added with the fill argument: http://stackoverflow.com/a/9862712/1000343 – Tyler Rinker Jun 22 '13 at 14:03
  • I've added some data. – Kaleb Jun 22 '13 at 15:02

2 Answers2

1

This isn't perfect as you don't have the speed in your data but it should get you to where you want with a few aesthetic changes:

Reshaping your data into a dataframe:

dat1 <- read.table(text="
Road    Start     End
1 157398137 166811234
1 216984017 238298694
1 238298694 247249719
2       0   8794530
2   8794530  26703134
2  59852594  73085123
2 206737339 218577503
2 218695178 231142027
3  96301465 115456078
3 116345621 126898764", header=T)

dat2 <- read.table(text="
Road    Start     End
       1 157398137 166811234
1 216984017 238298694
1 238298694 247249719
2     0   8794530
2   8794530  26703134
2  59852594  73085123
2 206737339 218577503
3   3469683  56797911
3  96301465 115456078
3 116345621 126898764", header=T)

dat3 <- read.table(text="
Road    Start     End
1 157398137 166811234
1 216984017 238298694
1 238298694 247249719
2       0   8794530
2   8794530  26703134
2  59852594  73085123
2 206737339 218577503
2 218695178 231142027
2 231142027 241946296
3   3469683  56797911
3  96301465 115456078
3 116345621 126898764", header=T)

lst <- list(dat1, dat2, dat3)

dat <- data.frame(Vehicle = rep(paste0("Vehicle", 1:3), sapply(lst, nrow)),
do.call(rbind, lst))

dat$Road <- factor(dat$Road)

dat

Plotting the data:

library(ggplot2)
ggplot(dat) + 
    geom_segment(aes(x=Start, xend=End, y=Vehicle, yend=Vehicle), size=3) +
    coord_flip() +
    facet_grid(Road~.)

enter image description here

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
1

Using geom_rect:

Vehicles <- structure(list(Road = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L), Start = c(157398137L, 216984017L, 238298694L, 
0L, 8794530L, 59852594L, 206737339L, 218695178L, 96301465L, 116345621L, 
157398137L, 216984017L, 238298694L, 0L, 8794530L, 59852594L, 
206737339L, 3469683L, 96301465L, 116345621L, 157398137L, 216984017L, 
238298694L, 0L, 8794530L, 59852594L, 206737339L, 218695178L, 
231142027L, 3469683L, 96301465L, 116345621L), End = c(166811234L, 
238298694L, 247249719L, 8794530L, 26703134L, 73085123L, 218577503L, 
231142027L, 115456078L, 126898764L, 166811234L, 238298694L, 247249719L, 
8794530L, 26703134L, 73085123L, 218577503L, 56797911L, 115456078L, 
126898764L, 166811234L, 238298694L, 247249719L, 8794530L, 26703134L, 
73085123L, 218577503L, 231142027L, 241946296L, 56797911L, 115456078L, 
126898764L), Vehicle = c("1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3")), .Names = c("Road", 
"Start", "End", "Vehicle"), row.names = c(NA, 32L), class = "data.frame")

set.seed(42)
Vehicles$overspeed <- runif(nrow(Vehicles),0,50)
Vehicles$overspeed <- cut(Vehicles$overspeed,c(0,20,30,Inf))

roads <- read.table(text="Road    Length
1   529290651
2   249139323
3   298024420",header=TRUE)

library(ggplot2)
p <- ggplot(Vehicles,aes(xmin=as.numeric(Vehicle)-0.4,
                         xmax=as.numeric(Vehicle)+0.4,
                         ymin=Start,ymax=End,fill=overspeed)) + 
  geom_rect() +
  facet_grid(Road~.,scales = "free_y") +
  #ugly hack to get correct road lengths:
  geom_rect(data=roads,aes(ymin=0,ymax=Length,xmin=0.4,xmax=1.4,fill=NA),alpha=0) + 
  scale_y_continuous(expand=c(0,1)) +
  xlab("Vehicle") +
  ylab("Distance") +
  theme_bw() +
  scale_fill_manual(values=c("(0,20]"="blue","(20,30]"="yellow","(30,Inf]"="red"))
print(p)

enter image description here

Edit: Following @Tyler Rinker's comment, you can reverse the scale like this:

p + scale_y_reverse(expand=c(0,1))
Roland
  • 127,288
  • 10
  • 191
  • 288
  • How do I change this so that Distance goes from lowest (at top) to farthest (at bottom) for each road? – Kaleb Jun 22 '13 at 17:55
  • 1
    This is a better approach than mine. Use: `scale_x_reverse(...)` – Tyler Rinker Jun 22 '13 at 18:27
  • @TylerRinker How can I change this so that the lengths of the rectangles plotted by `geom_rect()` are proportional to the corresponding road lengths? Therefore, the length of the rectangle for road 1 will be `529290651/249139323 =` 2.124477 times the length of the rectangle for road 2, etc. – Kaleb Jul 05 '13 at 23:45
  • I'm on a cell phone so I'm limited but use ?facet_grid The argument scales="tree" I think is what you're after. – Tyler Rinker Jul 06 '13 at 13:54