如何在ggplot2中颠倒y轴的顺序值

如何在ggplot2中颠倒y轴的顺序值

本文介绍了如何在ggplot2中颠倒y轴的顺序值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据框y。我正在尝试使用ggplot创建一个热图。我想颠倒y轴的顺序。最高值应该是00:00,值应该递减到23:59。例如,最高值应该是00:00,然后是01:00,02:00和03:00等等。现在,y轴从01:00开始,然后是02:00,03:00等。

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


推荐答案

由于ggplot2会将您的y轴值转换为一个因素,只是提前将所有值转换为利息因子:

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)

这篇关于如何在ggplot2中颠倒y轴的顺序值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:27