我想使用ggplot绘制下面场景的网格图,我试图在下面的图片中描述......我可以使用一些关于如何逻辑思考这种方法的指导.谢谢你的指导.
下面示例图片中的每个过道都有一个奇数侧和一个偶数侧
奇数侧
的空格从1 ... K开始上升,其中K是奇数
偶数侧的空格从2 ... N开始列出N是偶数
这种模式存在于停车场的每个过道
如果汽车停在太空中 - 我们会在数据库中跟踪该位置.
如何重现网格级ggplot以在图表上用符号表示停放汽车的所有空间?
占用空间的列表将通过.csv文件"馈送"到ggplot逻辑中:.csv的格式如下所示:
A01
A04
A05
A08
A09
A15
A20
A33
B07
B31
B44
C01
C04
C36
......
图片来源:Michael Layefsky,2010,Google Images
我直接使用的经验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="")
如果您在所显示的表单中仅列出了已占用的点作为输入的列表,然后您想要使用生成占用点的可视化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)
将"空格"拆分为有意义的坐标.我保留原始空间名称以便以后标记.以下是用于正确设置绘图的所有操作.
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]))
aisle
在将A转换为1,B转换为2之后,我将乘以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")