使用ggplot绘制停车场中占用的停车位

时间:2016-01-01 20:52:41

标签: r ggplot2

我想使用ggplot绘制以下场景的网格图,我试图在下面的图片中描述......我可以使用一些关于如何逻辑思考这种方法的指导。感谢您的指导。

-

下面示例图片中的每个过道都有一个奇数侧和一个偶数侧
奇数侧的空间从1 ... K上升,其中K是奇数
偶数侧的空间从2 ... N上升,其中N是偶数
这种模式存在于停车场的每个过道

如果汽车停在太空中 - 我们会在数据库中跟踪该位置。

如何重现网格级ggplot以在图表上用符号表示停放汽车的所有空间?

Parking Lot Example

占用空间的列表将通过.csv文件“馈送”到ggplot逻辑中:.csv的格式如下所示:

A01
A04
A05
A08
A09
A15
A20
A33
B07
B31
B44
C01
C04
C36
...

图片来源:Michael Layefsky,2010,Google Images

2 个答案:

答案 0 :(得分:9)

我直接使用grid的经验是有限的,所以我不能说grid函数有多难,但在ggplot2中似乎相当简单。这是一个简单的例子(我希望)与你想要的东西相差不远:

library(ggplot2)

# Set up grid of space identifiers
df = data.frame(y=1:10, x=rep(c(0:1, 3:4, 6:7), each=10),
                space=paste0(rep(c("A","B","C"), each=20), 
                            rep(c(seq(2,20,2),seq(1,20,2)), 3)), 
                stringsAsFactors=FALSE)

# Assume we have a vector of occupied spaces
set.seed(194)
occupied = sample(df$space, 30)

# Mark occupied spaces in data frame
df$status = ifelse(df$space %in% occupied, "Occupied", "Available")

ggplot(df) +
  geom_segment(aes(x=x - 0.5, xend=x + 0.5, y=y, yend=y - 1)) +
  geom_label(aes(label=space, x=x, y=y, fill=status), colour="blue", label.size=0) +
  annotate(geom="segment", x=seq(0.5,6.5,3), xend=seq(0.5,6.5,3), 
           y=rep(0,3), yend=rep(10,3), lty="11") +
  theme_bw(base_size=14) +
  scale_fill_manual(values=c(hcl(c(105,15),100,65))) +
  #scale_fill_manual(values=c(NA, hcl(15,100,65))) +    # Color only occupied spaces
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank()) +
  labs(x="",y="",fill="")

enter image description here

答案 1 :(得分:8)

如果您在所显示的表单中仅列出了已占用的点作为输入的列表,然后您想使用ggplot2生成占用点的可视化,则此方法将起作用。首先,我处理输入,将其转换为我可以轻松提供ggplot的内容。

# the provided example data
d <- read.table(text="
A01 
A04 
A05 
A08 
A09 
A15 
A20 
A33 
B07 
B31 
B44 
C01 
C04 
C36", stringsAsFactors=FALSE)

拆分&#34;空格&#34;进入有意义的坐标我保留原始空间名称以便以后标记。以下是用于正确设置绘图的所有操作。

cars <- strsplit(d[,1], "(?<=[A-Z])", perl=TRUE) # split the raw data
# turn resulting list into data.frame and give it names
cars <- setNames(do.call(rbind.data.frame, cars), c("aisle","spot.num"))
# convert the from factors to numeric, 
# and turn the aisle letter into numeric data for plotting 
# retain the original spot id for labeling the plot 
cars <- with(cars, data.frame(
  spot.num = as.numeric(as.character(spot.num)),
  aisle = aisle, # keep this around for faceting
  aisle.coord = 2 * (utf8ToInt(paste(as.character(aisle), collapse="")) - utf8ToInt("A")),
  spot.id = d[,1]))

在将A转换为1,B转换为2之后,我将aisle乘以2,依此类推,以生成一个名为aisle.coord的新变量。乘以2的原因是设置一个变量,其中每个过道可以由两行组成:

# if the spot number is even, increment aisle by 1 (put it on the right).
# This is possible because I multiplied by 2 earlier
cars$aisle.coord[cars$spot.num %% 2 == 0] <- cars$aisle.coord[cars$spot.num %% 2 == 0] + 1 
# We need to adjust the spot numbers to real row numbers
# i.e. A02 is in row 1, not row 2, A10 is in row 5, etc.
cars$spot <- ceiling(cars$spot.num / 2)

现在,绘图:

library(ggplot2)
library(grid) # for unit()

ggplot(cars, aes(x = aisle.coord %% 2, y = spot)) + 
  geom_tile(width = 0.5, height = 0.8) +
  facet_grid(~aisle) + 
  geom_text( aes(x = aisle.coord %% 2, y = spot, label = spot.id), color = "white")

这是对图表的一种简单尝试。有很多空间可供您改进和调整。这是另一种尝试,需要更多的努力。仍然有足够的调整空间(例如,您可以调整情节,以便显示整个批次,而不仅仅是批次的一部分到达最大点:B44):

ggplot(cars, aes(x = aisle.coord %% 2, y = spot)) + 
  geom_tile(width = 0.5, height = 0.8, fill = "orange") +
  facet_grid(~aisle) + 
  geom_text( aes(x = aisle.coord %% 2, y = spot, label = spot.id), color = "white", size = 4) +
  annotate("rect", ymin = 0, ymax = max(cars$spot)+0.5, xmin = 0.3, xmax = 0.7, fill = "grey40") +
  theme(panel.margin.x = unit(0.05, "lines"), 
    plot.background = element_rect("grey40"),
    panel.background = element_rect("grey40"),
    panel.grid.minor = element_blank(),
    axis.title = element_blank(),
    axis.text = element_blank(),
    strip.text = element_blank(),
    strip.background = element_blank()) +
  scale_y_continuous(breaks = seq(0.5, (max(cars$spot) + 0.5), 1)) +
  scale_x_continuous(breaks = c(-0.3, 1.3)) +
  geom_text(data=data.frame(x = 0.5, y = 10, aisle = LETTERS[1:length(unique(cars$aisle))]),
    aes(x = x, y = y, label = aisle), inherit.aes = FALSE, color = "white") 

enter image description here