Aisha Ali
Aisha Ali

Reputation: 11

Highlighting the geom tile of 1 city based on date on x axis

I am trying to give border to total of 2 geom tiles in ggplot2 with different colors. i have 5 cities on y axis and dates as in format "mmm-YY"on x axis from Mar-20 till Nov-20. Out of 5 cities, i want to give black color border to Aug-2020 tile of city-1 and red color border to Jul-2020 of city-2. so far i tried to add border to one of the cities i.e. city-1.

I have tried the below code but didn't got the expected outcome

IV$month <- as.Date(IV$month)

ggplot(IV, aes(x=month, y=City, fill=count)) +
  geom_tile(colour="black", linewidth = 0.3) +
  scale_fill_gradient(low="white", high="#00823B", limits=c(0, 5))+ 
  geom_tile(data = subset(IV, City == "City-1" & month == "1/8/2020"), 
            aes(x = month, y = City), 
            color = "black", size = 1.5, fill = NA)

IV<- data.frame( month = c("Aug-20", "Apr-20", "Aug-20", "Jul-20", "Oct-20", "Sep-20", "Apr-20", "Aug-20", "Nov-20", "Nov-20", "Jan-21", "Jul-20", "Mar-20", "Apr-20", "May-20", "May-20", "Sep-20", "Apr-20", "Aug-20", "Sep-20", "Mar-20", "Jul-20", "Jun-20", "Mar-20", "Sep-20", "Oct-20"), City = c("City-1", "City-1", "City-2", "City-2", "City-2", "City-2", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-4", "City-4", "City-4", "City-4", "City-5", "City-5", "City-5", "City-5", "City-5"), count = c(5, 4, 1, 0, 4, 1, 1, 2, 3, 1, 4, 1, 1, 1, 2, 1, 3, 1, 1, 3, 1, 2, 1, 1, 1) )

Any help will be highly appreciated

Upvotes: 0

Views: 96

Answers (2)

Michiel Duvekot
Michiel Duvekot

Reputation: 1881

library(ggplot2)

IV<- data.frame( 
  month = c("Aug-20", "Apr-20", "Aug-20", "Jul-20", "Oct-20", "Sep-20", "Apr-20", "Aug-20", "Nov-20", "Nov-20", "Jan-21", "Jul-20", "Mar-20", "Apr-20", "May-20", "May-20", "Sep-20", "Apr-20", "Aug-20", "Sep-20", "Mar-20", "Jul-20", "Jun-20", "Mar-20", "Sep-20", "Oct-20"), 
  City = c("City-1", "City-1", "City-2", "City-2", "City-2", "City-2", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-3", "City-4", "City-4", "City-4", "City-4", "City-5", "City-5", "City-5", "City-5", "City-5"), 
  count = c(5, 4, 1, 0, 4, 1, 1, 2, 3, 1, 4, 1, 1, 1, 2, 1, 3, 1, 1, 3, 1, 2, 1, 1, 1, 1) )

IV$date <- as.Date(paste0("01-", stringr::str_replace(IV$month, "-2", "-202")), format = "%d-%b-%Y")

ggplot(IV, aes(x=date, y=City, fill=count)) +
  geom_tile(
    aes(
      colour = dplyr::case_when(
        City == "City-1" & month == "Aug-20" ~ I("red"),
        City == "City-2" & month == "Jul-20" ~ I("black"),
        .default = NA
        )
      ),
    size = 0.3,
    linewidth = 1) +
  scale_fill_gradient(low="white", high="#00823B", limits=c(0, 5))

  

Upvotes: 0

stefan
stefan

Reputation: 125388

Here is an option to achieve your desired result which uses lubridate to convert the dates, but used a factor with the correct order for plotting:

library(ggplot2)
library(lubridate)

# Convert to date
IV$month <- lubridate::my(IV$month)

# Convert back to a factor with the correct order
IV <- IV[order(IV$month), ]
IV$month <- factor(
  IV$month,
  levels = unique(IV$month),
  labels = format(unique(IV$month), "%b %y")
)

ggplot(IV, aes(x = month, y = district, fill = count)) +
  geom_tile(colour = "black", linewidth = 0.3) +
  scale_fill_gradient(low = "white", high = "#00823B", limits = c(0, 5)) +
  geom_tile(
    data = subset(IV, district == "City-1" & month == "Aug 20"),
    color = "black", linewidth = 1.5, fill = NA
  )

Upvotes: 0

Related Questions