ggplot2热力图,带有条件的颜色比例尺

3

我需要改变 R 中 ggplot 的比例尺颜色。

我的表格如下:

tt<-data.frame(C1=c(0.4,.5,.5, 0, .8,.8),C2=c(.5,.6,.7, 0, .7,.8), C3=c(.8,.7,.9, 0, .8,.7), 
  C4=c(rep(0,6)), C5=c(0.4,.6,.6, 0, .8,.8),C6=c(0.8,.7,.5, 0, .8,.8), C7=c(0.8,.6,.4, 0, .8,.8))

row.names(tt)<-paste("F", 1:6, sep='')
tt<-as.matrix(tt)

然后我进行reshape操作:

library(ggplot2)
library(reshape2)
tt_melt <- melt(tt)
tt_melt
colnames(tt_melt)<-c('fila', 'columna', 'performance')

我的图表是呼叫中心及其每个职位相关指标的一种表示方式:

ggplot(data=tt_melt,
       aes(x=columna, y=fila, fill=performance)) + geom_tile() + 
  geom_text(aes(label=performance), color='white') 
+ theme_minimal(base_size = 12, base_family = "")+
labs(title = 'Performance por posicion en el call')+
  scale_colour_manual(values = c("red","yellow", "green"))
scale_fill_gradient(low = "yellow",  high = "darkgreen")

这里输入图片描述

但它没有考虑比例。我需要将0显示为白色(因为这意味着那个区域没有人),其余部分从红色到绿色渐变。所以它并不是一个真正的连续比例尺(但应该是从红到绿的连续比例尺)。如何让ggplot使用比例尺?此外,如何创建带有值条件的比例尺?最后,如果有更好的方法,请告诉我。非常感谢。

2个回答

4
我希望代码格式是SO粘贴的副产品。我更喜欢更有结构的ggplot建设。
如果您生成离散的间隔,则可以拥有更多控制:
library(ggplot2)
library(reshape2) 

tt <- data.frame(C1=c(0.4,.5,.5, 0, .8,.8),
                 C2=c(.5,.6,.7, 0, .7,.8), 
                 C3=c(.8,.7,.9, 0, .8,.7), 
                 C4=c(rep(0,6)), 
                 C5=c(0.4,.6,.6, 0, .8,.8),
                 C6=c(0.8,.7,.5, 0, .8,.8), 
                 C7=c(0.8,.6,.4, 0, .8,.8))

row.names(tt) <- paste("F", 1:6, sep='')
tt <- as.matrix(tt)

tt_melt <- melt(tt) 
colnames(tt_melt) <- c('fila', 'columna', 'performance')

tt_melt$cut <- cut(tt_melt$performance, 
                   breaks=c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0),
                   labels=as.character(c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9)),
                   include.lowest=TRUE)

perf_cols <- c("white", colorRampPalette(c("red", "green"))( 9 ))
perf_text_cols <- c("black", rep("white", 9))

gg <- ggplot(data=tt_melt, aes(x=columna, y=fila, fill=cut))
gg <- gg + geom_tile()
gg <- gg + geom_text(aes(label=performance, color=cut)) 
gg <- gg + labs(title = 'Performance por posicion en el call')
gg <- gg + coord_equal()
gg <- gg + scale_colour_manual(values = perf_text_cols)
gg <- gg + scale_fill_manual(values=perf_cols)
gg <- gg + theme_minimal(base_size = 12, base_family = "")
gg <- gg + theme(legend.position="none")
gg

输入图像描述

您应该使用Color Brewer并更改渐变色。这种方法还可以确保文本在白色瓷砖上可见。此外,使用coord_equal可以使您避免调整高度/宽度以获得均匀的块。您还可以通过向geom_tile添加非颜色参数来为瓷砖添加边框。

为了保持0值为白色(根据您的评论),只需像最初一样使用非颜色即可:

gg <- ggplot(data=tt_melt, aes(x=columna, y=fila, fill=cut))
gg <- gg + geom_tile()
gg <- gg + geom_text(aes(label=performance), color="white") 
gg <- gg + labs(title = 'Performance por posicion en el call')
gg <- gg + coord_equal()
gg <- gg + scale_fill_manual(values=perf_cols)
gg <- gg + theme_minimal(base_size = 12, base_family = "")
gg <- gg + theme(legend.position="none")
gg

enter image description here


谢谢!我现在只需要删除图表中的0标签,但保留其余部分。你能帮我处理一下吗? - GabyLP
1
哦,那就把颜色改成“白色”吧。我以为你实际上需要显示0标签。我更新了答案以反映这一点。 - hrbrmstr
谢谢!这正是我想要的!你能推荐一个简单但完整的ggplot教程吗? - GabyLP
1
我会认真仔细地学习《R Graphics Cookbook》(http://www.cookbook-r.com/Graphs/),以便完全掌握ggplot。 - hrbrmstr
你为什么也喜欢 gg <- gg + ...; gg <- gg + ...; gg <- gg + ... 这种编程范式呢? - rawr
1
完全个人意见(承认我有点“不正常” :-))=> (a) 比在大型图形构建的末尾计算 + 符号要容易得多; (b) 从我拥有的片段复制时,出错的可能性要小得多; (c) 强制进行精神确认,每个“语法短语”调用都足够强大,值得独立存在; (d) 更容易在创建过程中对另一个变量进行插入式赋值; (e) 注释掉绘图构建步骤要容易得多。需要注意的是:我对 ggplot 中一串 + 和 dplyr/magrittr 中过长的 %>% 管道或嵌套的函数括号感到不舒服。 - hrbrmstr

1

尝试:

ggplot(data=tt_melt, aes(x=columna, y=fila, fill=performance))+
    geom_tile()+ 
    geom_text(aes(label=performance), color='white') +
    theme_minimal(base_size = 12, base_family = "")+
    labs(title = 'Performance por posicion en el call')+
    scale_colour_manual(values = c("red","yellow", "green"))+
    scale_fill_gradient(low = "yellow",  high = "darkgreen")

enter image description here

你的代码中只有打字错误。如果你想在ggplot代码中继续下一行,那么该行末尾应该有一个“+”符号。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接