Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

r - how to reverse order y-axis values in ggplot2

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")

 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")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...