绘制R中不同类别观察的可视化

时间:2013-06-22 13:23:46

标签: r visualization

使用R,我想绘制几个观测的地图(其中观察是由其登记板给出的特定车辆),沿着几条不同的道路超速定义的距离。我还想根据车辆行驶的速度限制(红色 - + 30英里/小时,黄色 - 20-30英里/小时,蓝色 - 0-20英里/小时)对观察结果进行颜色编码。

INPUT

  1. 每辆车:
    • 发现超速行驶的道路
    • 特定道路的位置(开始和结束),在该道路上超速(可在一条道路上多次)
    • 超速类别(如上所述的红色,黄色或蓝色)。
  2. 每条路
    • 道路的距离。
  3. 数据

    $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
    

    预期输出

    enter image description here

2 个答案:

答案 0 :(得分: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~.)

enter image description here

答案 1 :(得分: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)

enter image description here

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

p + scale_y_reverse(expand=c(0,1))