如何使ggplot彩色条在中点附近不对称地变化? [英] How to make ggplot colorbar change asymmetrically around midpoint?

查看:44
本文介绍了如何使ggplot彩色条在中点附近不对称地变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个存在自然中点的东西,其中许多值低于中点,而只有几个高于中点.我想使中点以上的值突出.但是,由于它们非常接近中点(由于数据的分布),因此很难看到它们.

I want to plot something where there is a natural midpoint, many values below the midpoint, and only a few above the midpoint. I want to make the values above the midpoint stand out. But because they are so close to the midpoint (by virtue of the distribution of the data), they are hard to see.

这里是一个例子:

library(ggplot2)
mtcars$qsec <- mtcars$qsec-21
sp2<-ggplot(mtcars, aes(x=wt, y=mpg, color=qsec)) + geom_point()
sp2+scale_color_gradient2(midpoint=0, low="blue", mid="white",
                          high="red", space ="Lab" )

我希望0到1之间的红色和0到-6之间的蓝色一样是红色.在保持秤连续的同时,我该怎么做?

I want reds between 0 and 1 to be just as red as blues between 0 and -6 are blue. How can I do this, while keeping my scales continuous?

还是离散化是唯一的选择?如果是这样,我可以在使一部分刻度离散的同时保持其一部分连续吗?

Or is discretization the only option? If so, can I keep part of the scale continuous while making part of it discrete?

推荐答案

正如Henrik所建议的, scale_color_gradientn 可以完成这项工作.需要做的额外计算是在1是最高值而0是最低值时计算中点,可以使用(midpoint-min(mtcars $ qsec))/(max(mtcars $qsec)-min(mtcars $ qsec)).

As suggested by Henrik, scale_color_gradientn does the job. The extra computation that needs to be done is to calculate the midpoint when 1 is the highest value and 0 is the lowest value, which can be accomplished using (midpoint-min(mtcars$qsec))/(max(mtcars$qsec)-min(mtcars$qsec)).

library(ggplot2)
mtcars$qsec <- mtcars$qsec-21
sp2<-ggplot(mtcars, aes(x=wt, y=mpg, color=qsec)) + geom_point()
midpoint <- 0
sp2+scale_color_gradientn( colours = c("red","white","blue"),
                           values=c(1.0, (midpoint-min(mtcars$qsec))/(max(mtcars$qsec)-min(mtcars$qsec)),0))

这篇关于如何使ggplot彩色条在中点附近不对称地变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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