2

I have this data frame y. I am trying to create a heatmap using ggplot. I would like to reverse the order of y-axis. The top value should be 00:00 and the values should be decrementing to 23:59. For example, the top value should be 00:00, then 01:00, 02:00, 03:00, etc. Rigth now, y-axis is going up from 01:00, then 02:00, 03:00 etc. Any ideas how I can do this in ggplot2?

 cols<-c("white","#F0FFFF","#BBFFFF","#00FFFF","#42C0FB","#1C86EE", "green","yellow","#C9821E", "#FF0000", "#FF0000") vals<-c(0,0.19,0.29,0.39, 0.49,0.59, 0.69, 0.79, 0.89, 0.90,1) brk<-c(20, 30, 40, 50, 60, 70, 80, 90, 100) ggplot(y,aes(DATE, Time, fill=CPU)) + geom_tile() + theme_bw() + scale_fill_gradientn(name="CPU Utilization", colours=cols, values=val, limits=c(0,100), breaks = brk) + guides(fill = guide_legend(keywidth = 5, keyheight = 1))+ scale_x_date(breaks = "1 days", labels=date_format("%a")) + coord_trans(y="reverse") 

\n

 dput(head(y,50)) structure(list(DATE = structure(c(15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809), class = "Date"), Time = c("20:14", "20:29", "20:44", "20:59", "21:14", "21:29", "21:44", "21:59", "22:14", "22:29", "22:44", "22:59", "23:14", "23:29", "23:44", "23:59", "00:14", "00:29", "00:44", "00:59", "01:14", "01:29", "01:44", "01:59", "02:14", "02:29", "02:44", "02:59", "03:14", "03:29", "03:44", "03:59", "04:14", "04:29", "04:44", "04:59", "05:14", "05:29", "05:44", "05:59", "06:14", "06:29", "06:44", "06:59", "07:14", "07:29", "07:44", "07:59", "08:14", "08:29"), CPU = c(30, 30, 30, 31, 30, 27, 25, 23, 24, 22, 20, 21, 22, 21, 20, 20, 20, 15, 17, 15, 20, 16, 15, 16, 15, 14, 14, 11, 12, 11, 19, 22, 22, 21, 19, 20, 20, 17, 16, 10, 9, 9, 9, 10, 12, 13, 14, 14, 16, 16)), .Names = c("DATE", "Time", "CPU"), row.names = 4205:4254, class = "data.frame") 
4
  • anybody? I cannot seem to find a solution on this. Commented Apr 25, 2013 at 20:29
  • 1
    First off, I can't currently run your code because (at least) some objects are missing (brk, cols, val). Second, y$Time gets silently converted to factor with levels in alphabetical order. Try converting it before plotting: y$Time = factor(y$Time, levels=sort(unique(y$Time), decreasing=TRUE)). Commented Apr 26, 2013 at 0:12
  • @bdemarest, I am sorry, I've updated the original post with those values. The reason I have time as POSIXct is that I can use scale_y_datetime function to only show %H and %M. Commented Apr 26, 2013 at 13:04
  • @bdemarest, this looks like this going to be workinb but I have one little problem. I need to scale my y-axis so that not all of the values show up, show time values for every 2 hours to make the y-axis more readable, any ideas? Commented Apr 26, 2013 at 13:26

1 Answer 1

3

Since ggplot2 converts your y-axis values to a factor, just convert all values to the factor of interest in advance:

library(ggplot2) y <- data.frame(DATE = structure(c(15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809, 15809), class = "Date"), Time = c("20:14", "20:29", "20:44", "20:59", "21:14", "21:29", "21:44", "21:59", "22:14", "22:29", "22:44", "22:59", "23:14", "23:29", "23:44", "23:59", "00:14", "00:29", "00:44", "00:59", "01:14", "01:29", "01:44", "01:59", "02:14", "02:29", "02:44", "02:59", "03:14", "03:29", "03:44", "03:59", "04:14", "04:29", "04:44", "04:59", "05:14", "05:29", "05:44", "05:59", "06:14", "06:29", "06:44", "06:59", "07:14", "07:29", "07:44", "07:59", "08:14", "08:29"), CPU = c(30, 30, 30, 31, 30, 27, 25, 23, 24, 22, 20, 21, 22, 21, 20, 20, 20, 15, 17, 15, 20, 16, 15, 16, 15, 14, 14, 11, 12, 11, 19, 22, 22, 21, 19, 20, 20, 17, 16, 10, 9, 9, 9, 10, 12, 13, 14, 14, 16, 16)) y$Time <- factor(y$Time, levels=rev(sort(y$Time))) cols<-c("white","#F0FFFF","#BBFFFF","#00FFFF","#42C0FB","#1C86EE", "green","yellow","#C9821E", "#FF0000", "#FF0000") vals<-c(0,0.19,0.29,0.39, 0.49,0.59, 0.69, 0.79, 0.89, 0.90,1) brk<-c(20, 30, 40, 50, 60, 70, 80, 90, 100) ggplot(y,aes(DATE, Time, fill=CPU)) + geom_tile() + theme_bw() + scale_fill_gradientn(name="CPU Utilization", colours=cols, values=vals, limits=c(0,100), breaks = brk) 

plot as shown

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.