ggplot中十分低的中等颜色 [英] Low, med high colors for deciles in ggplot

查看:159
本文介绍了ggplot中十分低的中等颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用ggplot有一个多边形类型的空间图。其中,多边形被绘制并且多边形的颜色由其重量决定。示例数据框看起来像 - (这里是数据文件

pre $ code> polyNr xy centroidX centroidY weight
1 4459425.25 5328202.595264193 4459675.25 5328202.595264193 -13.055709633886783
1 4459550.25 4459675.25 5328419.101615138 5328202.595264193 -13.055709633886783
1 4459800.25 4459675.25 5328419.101615138 5328202.595264193 -13.055709633886783
1 4459925.25 4459675.25 5328202.595264193 5328202.595264193 -13.055709633886783
1 4459800.25 4459675.25 5327986.088913247 5328202.595264193 -13.055709633886783
1 4459550.25 5327986.088913247 4459675.25 5328202.595264193 -13.055709633886783
2 4457550.25 5337512.3683548765 4457800.25 5337512.3683548765 -118.36760699572329
2 4457675.25 5337728.874705822 4457800.25 5337512.3683548765 -118.36 760699572329
2 4457925.25 4457800.25 5337728.874705822 5337512.3683548765 -118.36760699572329
2 4458050.25 4457800.25 5337512.3683548765 5337512.3683548765 -118.36760699572329
2 4457925.25 4457800.25 5337295.862003931 5337512.3683548765 -118.36760699572329
2 4457675.25 4457800.25 5337295.862003931 5337512.3683548765 -118.36760699572329
3分配4475175.25 5336862.849302039 4475425.25 5336862.849302039 -3.397375074455629
3 4475300.25 5337079.355652984 4475425.25 5336862.849302039 -3.397375074455629
3 4475550.25 5337079.355652984 4475425.25 5336862.849302039 -3.397375074455629
3 4475675.25 5336862.849302039 4475425.25 5336862.849302039 -3.397375074455629
3 4475550.25 5336646.342951093 4475425.25 5336862.849302039 -3.397375074455629
3 4475300.25 5336646.342951093 4475425.25 5336862.849302039 -3.397375074455629
4 4464675.25 5343358.039830423 4464925.25 5343358.03 9830423 -51.57522722796112
4分配4464800.25 4464925.25 5343574.546181369 5343358.039830423 -51.57522722796112
4分配4465050.25 4464925.25 5343574.546181369 5343358.039830423 -51.57522722796112
4分配4465175.25 4464925.25 5343358.039830423 5343358.039830423 -51.57522722796112
4分配4465050.25 4464925.25 5343141.533479477 5343358.039830423 -51.57522722796112
4 4464800.25 5343141.533479477 4464925.25 5343358.039830423 -51.57522722796112
3438 4459050.25 5338378.393758661 4459300.25 5338378.393758661 1.066256760712294
3438 4459175.25 5338594.900109607 4459300.25 5338378.393758661 1.066256760712294
3438 4459425.25 5338594.900109607 4459300.25 5338378.393758661 1.066256760712294
3438 4459550.25 5338378.393758661 4459300.25 5338378.393758661 1.066256760712294
3438 4459425.25 5338161.887407715 4459300.25 5338378.393758661 1.066256760712294
3438 4459175.25 5338161.887 407715 4459300.25 5338378.393758661 1.066256760712294

我的步骤是 -


  1. 将整个数据集划分为十进制数

    breaks = unique(quantile(df $ weight) ,probs = seq(0,1,by = 0.1)))
    df $ deciles = cut(df $ weight,breaks = breaks,include.lowest = TRUE)


  2. 色阶(我想要正数为红色,负数为绿色)

    库(RColorBrewer)
    colors = brewer.pal(name =RdYlGn,n = nlevels(df $ dec))
    names(colors)= rev(levels(df $ deciles) code $ $ b $ (ggplot2)
    ggplot(df,aes(x = x,y = y))+ geom_polygon(aes(group = polyNr,fill = factor(deciles)))+ scale_fill_manual(values = colors)


这给我一个情节 - 看起来像 - / b>



但是,我的其他要求是 - 我想要t o零点为白色。一般来说,我可以使用

  scale_fill_gradient2(低=静音(绿色),中等=白色,高=静音(红色),中点= 0)

但是,我不能使用它与我的离散比例。



首先,这可能吗?如果是的话,我怎样才能得到低中高颜色以及十分尺度。如果这是重复问题,请找到我错过的原始问题。



- 我为不同的数据集使用相同的代码,因此,设置手动颜色比例是不可取的。

编辑 -



对于颜色设置(红色,白色,绿色),我也试过colorRampPalette。 (感谢@ Pewi指出)
$ b

colors = colorRampPalette(c(red,white,green))( 11)



这给了我下面的情节。





仍然设置为零重量的白色是一个主要问题。我也在基础包中遇到了同样的问题。

解决方案

我将您的问题解释为如何创建n个颜色,范围从x为了穿过白色。对这个问题的一个答案是使用来自程序包 grDevices 的函数 colorRampPalette
$ b(b),b(b),b(b),c(b),b(b) = 21)

plot(1:21,col = colors,pch = 18,cex = 4)

如果你用类似的东西替换你的第2部分,你可能会得到所需的输出。



编辑:



恐怕这不会很优雅,但可以忍受我。

 #模拟数据
dat = data.frame(x = -3:9)

#切入十进制数
dat $ y = cut(dat $ x,breaks = quantile(dat $ x,seq(0,1,0.1)),include.lowest = T)

#找到最接近零的值是
dat $ part = as.numeric( (abs(0-dat $ x)== min(abs(dat $ x-0)))]])

#将颜色范围分成两部分
highcolours< - colorRampPalette(c(red,white))(n = sum(dat $ part == TRUE))
低色彩< - colorRampPalette(c(白色,绿色))(n = sum(dat $部分== FALSE)+1)

#combine颜色
cols = c (高级色彩,低色彩)

#高低包含中点(白色)删除其中一个
cols = cols [!重复(cols)]

#示例
plot(1:nrow(dat),col = cols,pch = 18,cex = 4)


I want to have a polygon type of spatial plots using ggplot. In which, polygons are plotted and color of polygons are decided by its weight.

Sample data frame look like - (here is the data file CompleteDataFile)

    polyNr   x   y   centroidX   centroidY   weight
1   4459425.25  5328202.595264193   4459675.25  5328202.595264193   -13.055709633886783
1   4459550.25  5328419.101615138   4459675.25  5328202.595264193   -13.055709633886783
1   4459800.25  5328419.101615138   4459675.25  5328202.595264193   -13.055709633886783
1   4459925.25  5328202.595264193   4459675.25  5328202.595264193   -13.055709633886783
1   4459800.25  5327986.088913247   4459675.25  5328202.595264193   -13.055709633886783
1   4459550.25  5327986.088913247   4459675.25  5328202.595264193   -13.055709633886783
2   4457550.25  5337512.3683548765  4457800.25  5337512.3683548765  -118.36760699572329
2   4457675.25  5337728.874705822   4457800.25  5337512.3683548765  -118.36760699572329
2   4457925.25  5337728.874705822   4457800.25  5337512.3683548765  -118.36760699572329
2   4458050.25  5337512.3683548765  4457800.25  5337512.3683548765  -118.36760699572329
2   4457925.25  5337295.862003931   4457800.25  5337512.3683548765  -118.36760699572329
2   4457675.25  5337295.862003931   4457800.25  5337512.3683548765  -118.36760699572329
3   4475175.25  5336862.849302039   4475425.25  5336862.849302039   -3.397375074455629
3   4475300.25  5337079.355652984   4475425.25  5336862.849302039   -3.397375074455629
3   4475550.25  5337079.355652984   4475425.25  5336862.849302039   -3.397375074455629
3   4475675.25  5336862.849302039   4475425.25  5336862.849302039   -3.397375074455629
3   4475550.25  5336646.342951093   4475425.25  5336862.849302039   -3.397375074455629
3   4475300.25  5336646.342951093   4475425.25  5336862.849302039   -3.397375074455629
4   4464675.25  5343358.039830423   4464925.25  5343358.039830423   -51.57522722796112
4   4464800.25  5343574.546181369   4464925.25  5343358.039830423   -51.57522722796112
4   4465050.25  5343574.546181369   4464925.25  5343358.039830423   -51.57522722796112
4   4465175.25  5343358.039830423   4464925.25  5343358.039830423   -51.57522722796112
4   4465050.25  5343141.533479477   4464925.25  5343358.039830423   -51.57522722796112
4   4464800.25  5343141.533479477   4464925.25  5343358.039830423   -51.57522722796112
3438    4459050.25  5338378.393758661   4459300.25  5338378.393758661   1.066256760712294
3438    4459175.25  5338594.900109607   4459300.25  5338378.393758661   1.066256760712294
3438    4459425.25  5338594.900109607   4459300.25  5338378.393758661   1.066256760712294
3438    4459550.25  5338378.393758661   4459300.25  5338378.393758661   1.066256760712294
3438    4459425.25  5338161.887407715   4459300.25  5338378.393758661   1.066256760712294
3438    4459175.25  5338161.887407715   4459300.25  5338378.393758661   1.066256760712294

My steps are -

  1. Divide the whole data set into deciles as

    breaks=unique(quantile(df$weight,probs=seq(0,1,by=0.1))) df$deciles = cut(df$weight,breaks=breaks,include.lowest=TRUE)

  2. color scale ( I want to have positive numbers as red and negative as green)

    library(RColorBrewer) colours=brewer.pal(name="RdYlGn", n=nlevels(df$deciles)) names(colours)=rev(levels(df$deciles))

  3. plot

    library(ggplot2) ggplot(df,aes(x=x,y=y)) + geom_polygon(aes(group=polyNr,fill=factor(deciles))) + scale_fill_manual(values=colours)

This gives me a plot - look like -

But, my other requirement is - I want to have zero as white. In general, I can do this using

scale_fill_gradient2(low = muted("green"), mid = "white", high = muted("red"), midpoint = 0,)

But, I can't not use it with my discrete scale.

So first, Is that possible ? If yes, how can I get the low high mid colors along with decile scales. If this is duplicate question please locate the original question which I have missed.

P.S. - I am using the same code for different dataset, thus, setting manual color scale is not preferable.

EDIT -

For the color settings (red,white,green) I tried colorRampPalette also. (Thanks to @ Pewi for pointing out)

colours = colorRampPalette(c("red", "white", "green"))(11)

This give me the following plot.

Still setting of white color for zero weight is a major problem. I encountered the same issue in base package also.

解决方案

I interpret your question as "how do I create n colours ranging from x to y passing through white". One answer to that question would be to use the function colorRampPalettefrom package grDevices

library(grDevices)

colours <- colorRampPalette(c("red", "white", "green"))(n = 21)

plot(1:21, col=colours,pch=18,cex=4)

If you replace your part 2 with something similar you will probably be able to get the desired output.

Edit:

I am afraid this won't be very elegant but bear with me.

#sim data
dat = data.frame(x =-3:9)

#cut into deciles
dat$y = cut(dat$x,breaks=quantile(dat$x,seq(0,1,0.1)),include.lowest = T)

#Find in wich decile the value closest to zero is
dat$part = as.numeric(dat$y) <= as.numeric(dat$y[which(abs(0-dat$x)==min(abs(dat$x-0)))])

#split color range into two parts
highcolours <- colorRampPalette(c("red","white"))(n = sum(dat$part==TRUE))
lowcolours <- colorRampPalette(c("white","green"))(n = sum(dat$part==FALSE)+1)

#combine colors
cols = c(highcolours,lowcolours)

#both high and low contain midpoint (white) remove one of them
cols = cols[!duplicated(cols)]

#Example 
plot(1:nrow(dat), col=cols ,pch=18,cex=4)

这篇关于ggplot中十分低的中等颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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