Reputation: 2256
I have a heatmap with so many rows (thousands of cities) that for clarity I only want to show the names of a few of them. I still want to show the whole heatmap as the colors give a sense of the situation (the city-names are not important but I want to show a few of them for pedagogical purposes).
library(ggplot2)
n <- 15
c.1 <- c(rep(x="Summer", times=n), rep(x="Winter", times=n))
c.2 <- c(rep(x="Dallas", times=n/5), rep(x="Seattle", times=n/5), rep(x="Atlanta", times=n/5), rep(x="Chicago", times=n/5), rep(x="Boston", times=n/5))
c.3 <- c("Morning", "Midday", "Evening")
to.plot <- data.frame(cbind(c.1, c.2, c.3))
to.plot$value <- sample(rep(x=c("Bad", "Average", "Good"), times=100), 10)
colnames(to.plot) <- c("Season", "City", "Time", "value")
to.plot$City <- factor(to.plot$City, levels=c("Seattle", "Chicago", "Dallas", "Atlanta", "Boston"), ordered=TRUE)
p <- ggplot(to.plot, aes(x=Season, y=City))
p <- p + geom_tile(aes(fill=value), colour="white")
p <- p + scale_fill_manual(values=c("red", "green", "yellow"))
p <- p + theme(legend.position = "none", axis.text.x=element_text(angle=90, 8))
p <- p + facet_grid(. ~ Time)
p <- p + theme(legend.position = "none")
print(p)
In the example-plot with only five cities it is easy to see all five city-names, but in the real example with thousands of cities they blur together.
How can I see exactly the same heatmap, but with only every third or so city-name displayed? I included ordered factors because order is relevant in the plot visualization (factorization may be why I have the problem, but the factor-orders must be there).
Upvotes: 1
Views: 1131
Reputation: 899
If you create a vector containing the cities you want to label by sampling from the levels of your city variable:
breakpoints <- levels(to.plot$City)[seq(1, length(levels(to.plot$City)), 2)]
Adjusting "2" determines how many labels, you might want to fiddle until you get something you like.
then at the end of your code you add:
p <- p + scale_y_discrete(breaks = breakpoints)
print(p)
To tell ggplot where to put the y axis breaks using the new vector. I think this still preserves the order of the factor too?
Does that help?
(Partly thanks to nico's answer to Extracting every nth element of a vector)
Upvotes: 2