Reputation: 10646
structure(list(Date = structure(c(1372698000, 1291129200, 1291388400,
1298646000, 1386007200, 1295017200, 1382104800, 1385240400, 1265986800,
1364835600), class = c("POSIXct", "POSIXt"), tzone = ""), Logons = c(" 973,675",
" 710,782", " 734,635", " 812,793", "1,126,432", " 916,832",
"1,011,911", "1,974,513", " 674,884", "1,278,295"), Month = structure(c(7L,
11L, 12L, 2L, 12L, 1L, 10L, 11L, 2L, 4L), .Label = c("January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"), class = "factor"),
Max = c(973675L, 710782L, 734635L, 812793L, 1126432L, 916832L,
1011911L, 1974513L, 674884L, 1278295L), Year = c("2013",
"2010", "2010", "2011", "2013", "2011", "2013", "2013", "2010",
"2013")), .Names = c("Date", "Logons", "Month", "Max", "Year"
), row.names = c(453L, 2564L, 2636L, 3343L, 3435L, 4545L, 5275L,
5786L, 7382L, 8077L), class = "data.frame")
I am trying to create a heatmap where Year will be in y-axix and Month will be in x-axis.
I am doing this:
ggplot(y ,aes(Month, Year, fill=Logons, label=paste(paste(weekdays(Date), format(Date,"%H:%M"), sep="\n"), "\n",Logons))) + geom_tile() + theme_bw() + guides(fill = guide_legend(keywidth = 5, keyheight = 1)) + theme(axis.text.x = element_text(size=10, angle=45, hjust=1)) + geom_text(size=3)+ scale_colour_manual(breaks = c(200000, 400000, 800000, 1000000, 1300000, 1500000), labels = c("0 month", "1 month", "3 months","6 months", "9 months", "12 months"),values = c("#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2", "#D55E00"))
Not working. I get bunch of items on my legend, I only need 6 items and 6 color codes in my heatmap. Any ideas why this is not working?
Upvotes: 0
Views: 378
Reputation: 98579
First problem in your data is that Logons
are treated as factor and not as numeric because there are commas inside numbers. So you have to convert them to numeric.
y$Logons<-as.numeric(gsub(",","",y$Logons))
As you have used fill=
for geom_tile()
then you have to use scale_fill_...
to change fill values. In this case use scale_fill_gradientn()
to format your fill values.
ggplot(y ,aes(Month, Year, fill=Logons,
label=paste(paste(weekdays(Date), format(Date,"%H:%M"), sep="\n"), "\n",Logons))) +
geom_tile() + theme_bw() +
guides(fill = guide_legend(keywidth = 5, keyheight = 1)) +
theme(axis.text.x = element_text(size=10, angle=45, hjust=1)) +
geom_text(size=3)+
scale_fill_gradientn(limits=c(200000, 1500000),
breaks = c(200000, 400000, 800000, 1000000, 1300000, 1500000),
labels = c("0 month", "1 month", "3 months","6 months", "9 months", "12 months"),
colours = c("#E69F00", "#56B4E9", "#009E73","#F0E442", "#0072B2", "#D55E00"))
Also I think you should remove guides(fill = guide_legend(keywidth = 5, keyheight = 1))
line from your code because fill colors change as gradient but now you get discrete values in legend that won't exactly match colors in plot.
Upvotes: 2