ggplot 中十分位数的低、中高颜色
Low, med high colors for deciles in ggplot
我想使用 ggplot 绘制多边形类型的空间图。其中,绘制多边形,多边形的颜色由其权重决定。
示例数据框看起来像-(这里是数据文件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
我的步骤是 -
将整个数据集分成十分位数为
breaks=unique(quantile(df$weight,probs=seq(0,1,by=0.1)))
df$deciles = cut(df$weight,breaks=breaks,include.lowest=TRUE)
色标(我希望正数为红色,负数为绿色)
library(RColorBrewer)
colours=brewer.pal(name="RdYlGn", n=nlevels(df$deciles))
names(colours)=rev(levels(df$deciles))
情节
library(ggplot2)
ggplot(df,aes(x=x,y=y)) + geom_polygon(aes(group=polyNr,fill=factor(deciles))) + scale_fill_manual(values=colours)
这给了我一个情节 - 看起来像 -
但是,我的另一个要求是 - 我想要零作为白色。一般来说,我可以使用
scale_fill_gradient2(low = muted("green"), mid = "white", high = muted("red"), midpoint = 0,)
但是,我不能不将它与我的离散秤一起使用。
所以首先,这可能吗?如果是,我怎样才能获得低高中颜色以及十分位数。如果这是重复的问题,请找到我遗漏的原始问题。
P.S。 - 我对不同的数据集使用相同的代码,因此,设置手动色标是不可取的。
编辑 -
对于颜色设置(红色、白色、绿色),我也尝试了 colorRampPalette。 (感谢@Pewi指出)
colours = colorRampPalette(c("red", "white", "green"))(11)
这给了我下面的情节。
仍然为零权重设置白色 是一个主要问题。我在基本包中也遇到了同样的问题。
我将你的问题解释为 "how do I create n colours ranging from x to y passing through white"。该问题的一个答案是使用包 grDevices
中的函数 colorRampPalette
library(grDevices)
colours <- colorRampPalette(c("red", "white", "green"))(n = 21)
plot(1:21, col=colours,pch=18,cex=4)
如果您用类似的东西替换您的第 2 部分,您可能能够获得所需的输出。
编辑:
恐怕这不会很优雅,但请耐心等待。
#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)
我终于做到了。
df = read.table("sampleData.txt",header=T)
breaks=unique(quantile(df$weight,probs=seq(0,1,by=0.1)))
df$deciles = cut(df$weight,breaks=breaks,include.lowest=TRUE)
df$part = as.numeric(df$deciles) <= as.numeric(df$deciles[which(abs(0-df$weight)==min(abs(df$weight-0)))])
在这一步之后,我不能只拥有与数据点一样多的颜色。因此我按照 df$part
的比例划分色标
highCols = round (sum(df$part==FALSE)*10/(sum(df$part==FALSE)+sum(df$part==TRUE)))
lowCols = 10 - highCols
highColours = colorRampPalette(c("red","darkred"))(n = highCols)
lowColours = colorRampPalette(c("darkgreen","green","white"))(n=lowCols)
cols = c(lowColours,highColours)
cols = cols[!duplicated(cols)]
ggplot(df,aes(x=x,y=y)) + geom_polygon(aes(group=polyNr,fill=factor(deciles))) + scale_fill_manual(values=cols)
这给我剧情--
虽然剧情还是不错的,但从长远来看,如果可能的话,我更希望有更直接的方法。谢谢
我想使用 ggplot 绘制多边形类型的空间图。其中,绘制多边形,多边形的颜色由其权重决定。
示例数据框看起来像-(这里是数据文件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
我的步骤是 -
将整个数据集分成十分位数为
breaks=unique(quantile(df$weight,probs=seq(0,1,by=0.1))) df$deciles = cut(df$weight,breaks=breaks,include.lowest=TRUE)
色标(我希望正数为红色,负数为绿色)
library(RColorBrewer) colours=brewer.pal(name="RdYlGn", n=nlevels(df$deciles)) names(colours)=rev(levels(df$deciles))
情节
library(ggplot2) ggplot(df,aes(x=x,y=y)) + geom_polygon(aes(group=polyNr,fill=factor(deciles))) + scale_fill_manual(values=colours)
这给了我一个情节 - 看起来像 -
但是,我的另一个要求是 - 我想要零作为白色。一般来说,我可以使用
scale_fill_gradient2(low = muted("green"), mid = "white", high = muted("red"), midpoint = 0,)
但是,我不能不将它与我的离散秤一起使用。
所以首先,这可能吗?如果是,我怎样才能获得低高中颜色以及十分位数。如果这是重复的问题,请找到我遗漏的原始问题。
P.S。 - 我对不同的数据集使用相同的代码,因此,设置手动色标是不可取的。
编辑 -
对于颜色设置(红色、白色、绿色),我也尝试了 colorRampPalette。 (感谢@Pewi指出)
colours = colorRampPalette(c("red", "white", "green"))(11)
这给了我下面的情节。
仍然为零权重设置白色 是一个主要问题。我在基本包中也遇到了同样的问题。
我将你的问题解释为 "how do I create n colours ranging from x to y passing through white"。该问题的一个答案是使用包 grDevices
colorRampPalette
library(grDevices)
colours <- colorRampPalette(c("red", "white", "green"))(n = 21)
plot(1:21, col=colours,pch=18,cex=4)
如果您用类似的东西替换您的第 2 部分,您可能能够获得所需的输出。
编辑:
恐怕这不会很优雅,但请耐心等待。
#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)
我终于做到了。
df = read.table("sampleData.txt",header=T)
breaks=unique(quantile(df$weight,probs=seq(0,1,by=0.1)))
df$deciles = cut(df$weight,breaks=breaks,include.lowest=TRUE)
df$part = as.numeric(df$deciles) <= as.numeric(df$deciles[which(abs(0-df$weight)==min(abs(df$weight-0)))])
在这一步之后,我不能只拥有与数据点一样多的颜色。因此我按照 df$part
的比例划分色标highCols = round (sum(df$part==FALSE)*10/(sum(df$part==FALSE)+sum(df$part==TRUE)))
lowCols = 10 - highCols
highColours = colorRampPalette(c("red","darkred"))(n = highCols)
lowColours = colorRampPalette(c("darkgreen","green","white"))(n=lowCols)
cols = c(lowColours,highColours)
cols = cols[!duplicated(cols)]
ggplot(df,aes(x=x,y=y)) + geom_polygon(aes(group=polyNr,fill=factor(deciles))) + scale_fill_manual(values=cols)
这给我剧情--
虽然剧情还是不错的,但从长远来看,如果可能的话,我更希望有更直接的方法。谢谢