且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ggplot2热图将颜色分配给中断

更新时间:2022-12-15 11:27:53

嗯..我一定会错过一些东西。代码看起来不错。例如,如果我设定

  y $ CPU  $ c 



和图,

  ggplot( y,aes(DATE,Time,fill = CPU))+ geom_tile()+ theme_bw()+ 
scale_fill_gradientn(name =CPU Utilization,colors = pals,
values = vals,limits = c (0,100),break = brk)

我得到以下图表,它符合您的要求:


I'm tring to create a heatmap using ggplot to color 15 cpu averages for a server. The data is for 24 hours for each day in 15 minute increments. Need to show different color if the cpu utilization within 20%, 30%, 40% etc. When I do the below, my chart does not look right. It looks like the colors are representing different cpu utilization. Below is the a sample data. Any idea, how I can tackle this?

dput(head(y,20))
structure(list(DATE = structure(c(15795, 15795, 15795, 15795, 
15795, 15795, 15795, 15795, 15795, 15795, 15795, 15795, 15795, 
15795, 15795, 15795, 15795, 15795, 15795, 15795), 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"
    ), CPU = c(34.2, 34.6, 33.9333333333333, 33.2666666666667, 
    36.6333333333333, 33.1333333333333, 27.2666666666667, 26.4333333333333, 
    27, 26.6, 26.3666666666667, 26.5666666666667, 26.0666666666667, 
    24.9333333333333, 24.9333333333333, 25.3, 28.4857142857143, 
    23.5, 20.8, 19.2666666666667)), .Names = c("DATE", "Time", 
"CPU"), row.names = 2861:2880, class = "data.frame")

pals<-c("#F0FFFF","#F0FFFF","#AEEEEE","#ADD8E6","#00BFFF","green","#E4F224","#C9821E", "#FF0000")
vals<-c(0.19,0.29,0.39,0.49,0.59,0.69,0.79,0.89,0.99)
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=pals,  
                           values=vals,limits=c(0,100), breaks = brk) 

Hmm .. I must be missing something. The code looks good to me. For e.g., if I set

y$CPU <- seq(5,100,5)

And plot,

ggplot(y, aes(DATE, Time, fill=CPU)) + geom_tile() + theme_bw() + 
       scale_fill_gradientn(name="CPU Utilization", colours=pals,  
                            values=vals,limits=c(0,100), breaks = brk)

I get the following plot, which matches your requirement: