Using ggplot to park occupied parking spaces

I like to use ggplot to build the grid in the following scenario, which Ive tried to depict in the image below ... I could use some recommendations on how to think logically about the approach. Thanks for the guide.

-

Each pass in the example example below has an odd-numbered side and an even digital side
The spaces on the odd side are listed in increasing order from 1 ... K, where K is odd
The spaces on the even side are listed as ascending from 2 ... N, where N is
This pattern exists for each aisle in the parking lot.

If the car is in space - we track this place in the database.

How can I play ggplot at the grid level to indicate with the symbol on the chart all the places where the car is?

Parking example

The list of occupied spaces will be fed into the ggplot logic via the .csv file: the .csv format will look something like this:

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

Image Credit: Michael Layefsky, 2010, Google Images

+6
source share
2 answers

If you accept a list of only busy points as input in the form you showed, and then you want to create a visualization of busy points with ggplot2 , this approach will work. First, I handle the input, turning it into something that I can easily give 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) 

Separate the spaces into meaningful coordinates. I kept the original space names for later labeling. The following is all the manipulations used to properly configure the schedule.

 cars <- strsplit(d[,1], "(?<=[AZ])", 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])) 

I multiplied aisle by 2 after converting A to 1, B to 2, etc., to create a new variable called aisle.coord . The reason for multiplying by 2 is to set a variable in which each pass can consist of two lines:

 # 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 # ie A02 is in row 1, not row 2, A10 is in row 5, etc. cars$spot <- ceiling(cars$spot.num / 2) 

Now, plotting:

 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") 

This is the head on the chart. Lots of room for you to improve and customize it. Here is another attempt with less effort. However, there is enough room for tuning (for example, you can adjust the schedule so that a full batch appears, and not just a part of the batch to the maximum place: 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

+7
source

My experience with using grid directly is limited, so I can't say how complicated it would be with grid functions, but it seems reasonably simple in ggplot2 . Here is a simple example that (I hope) is not too far from what you are looking for:

 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

+9
source

All Articles