ggplot2热图将颜色分配给中断 [英] ggplot2 heatmap to assign colors to breaks

查看:121
本文介绍了ggplot2热图将颜色分配给中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用ggplot为服务器创建15个cpu平均值的热图。数据为每天15分钟24小时。如果CPU利用率在20%,30%,40%等内,则需要显示不同的颜色。当我执行以下操作时,我的图表看起来不正确。它看起来像颜色代表不同的CPU利用率。以下是一个示例数据。任何想法,我该如何解决这个问题?

  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, ),
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。 (#F0FFFF,#F0FFFF,#AEEEEE,#ADD8E6,#00BFFF,g reen,#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,colors = pals,
values = vals,limits = c(0,100),breaks = brk)


解决方案

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

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



和图,

  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:

这篇关于ggplot2热图将颜色分配给中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆