user1471980
user1471980

Reputation: 10636

How do you insert text in tiles that dont overlap in ggplot2

I am trying to create heat map in ggplot2. This is my data frame called t:

structure(list(Product = structure(c(1L, 4L, 3L, 2L), .Label = c("Aplication, Database, Servers, Inf", 
"Business", "IT", "Operations"), class = "factor"), Day = structure(c(1L, 
1L, 2L, 1L), .Label = c("Tues", "Wed"), class = "factor"), Month = structure(c(1L, 
1L, 2L, 3L), .Label = c("5/1/2015", "6/1/2015", "7/1/2015"), class = "factor"), 
    Total = c(5L, 5L, 3L, 2L)), .Names = c("Product", "Day", 
"Month", "Total"), class = "data.frame", row.names = c(NA, -4L
))

When the same products belong to the same time frame, geom_text puts text on top of each other. Is there a way to do a new line character so that text are not put on top of each other in geom_text?

This is my ggplot2 code to create the heatmap:

ggplot(t, aes(x=Month, y=Day)) + geom_tile(aes(order=Day, fill=Total), limits=c("Mon","Fri"),size=1)+
  theme(legend.position=c("none"))+
  geom_text(size=2.5,aes(label=Product))

Upvotes: 0

Views: 347

Answers (1)

MrFlick
MrFlick

Reputation: 206308

It would probably be easiest if you collapsed the data via your own transformation of the data. For example

ggplot(t, aes(x=Month, y=Day)) + 
  geom_tile(aes(order=Day, fill=Total), limits=c("Mon","Fri"),size=1)+
  theme(legend.position=c("none"))+
  geom_text(data=aggregate(Product~Month+Day, t, paste, collapse="\n"), 
    aes(label=Product), size=2.5)

to get

enter image description here

Upvotes: 1

Related Questions