-1

使用 R,我想绘制沿着几条不同道路在规定距离内超速行驶的多个观测值的地图(其中观测值是由其车牌给出的特定车辆)。我还想根据车辆行驶的速度限制(红色-+30mph,黄色-20-30mph,蓝色-0-20mph)对观察结果进行颜色编码。

输入

  1. 对于每辆车:
    • 发现它超速行驶的道路
    • 它正在超速行驶的给定道路的位置(起点和终点)(可以在一条道路上多次)
    • 超速类别(红色、黄色或蓝色如上)。
  2. 对于每条道路
    • 道路的距离。

数据

$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

预期输出

在此处输入图像描述

4

2 回答 2

1

使用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)

在此处输入图像描述

编辑:根据@Tyler Rinker 的评论,您可以像这样反转比例:

p + scale_y_reverse(expand=c(0,1))
于 2013-06-22T16:17:24.850 回答
1

这并不完美,因为您的数据速度不快,但它应该可以通过一些美学变化将您带到您想要的位置:

将数据重塑为数据框:

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

绘制数据:

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

在此处输入图像描述

于 2013-06-22T15:28:11.410 回答