Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ggplot to plot occupied parking spaces in a parking lot

Tags:

r

ggplot2

I’d like to use ggplot to draw a grid plot of the following scenario which I’ve attempted to depict in the picture below... I could use some guidance on how to logically think about the approach. Thank you for the guidance.

--

Each aisle in the example picture below has an odd number side—and an even number side
The spaces on the odd-side are listed ascending from 1… K where K is odd
The spaces on the even-side are listed ascending from 2…N where N is even
This pattern exists for each aisle in the parking lot

If a car is parked in a space—we track that spot in a database.

How can I reproduce a grid-level ggplot to indicate with a symbol on the plot all spaces where a car is parked?

Parking Lot Example

The listing of occupied spaces would be “fed” into the ggplot logic via a .csv file: the format of the .csv would 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

like image 485
user5509057 Avatar asked Jan 01 '16 20:01

user5509057


1 Answers

My experience with direct use of grid is limited, so I can't say how hard this would be with grid functions, but it seems reasonably straightforward in ggplot2. Here's a simple example that is (I hope) not too far off from what you're 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

like image 110
eipi10 Avatar answered Jan 04 '23 19:01

eipi10