简体   繁体   English

基于R中数据密度的着色

[英]Coloring based on data density in R

I have a data frame which every row has a numeric column. 我有一个数据框,每一行都有一个数字列。 I want to allocate different colors based on this column. 我想根据此列分配不同的颜色。 The numbers of this column range from 1 to 152. I used this function: 该列的数字范围是1到152。我使用了以下功能:

colors <- function(df,sp) {
check<-colorRampPalette(c("green","red"))(152)
color <- c() 
for (i in 1:nrow(sp))
{
  color <-c(color, check[df$n[df$id==as.numeric(sp[i,]$ID)])
} 
return(color)
} 

So the color is divided equally into 152 pieces.However, data in this column are mostly small (mostly are 1), the histogram is like this 因此颜色被平均分为152个。但是,此列中的数据很少(大部分为1),直方图如下所示

在此处输入图片说明

If I used this code the color is mostly green-like and hardly see a red-like. 如果我使用此代码,则颜色通常是绿色,几乎看不到红色。

I am using this function to change the line color in the leaflet: 我正在使用此功能来更改传单中的线条颜色:

leaflet() %>%
...
addPolylines(data = data,weight = '1', color=colors(nodes,data))

So how can I change this function to make the color can be evenly distributed, so it can generate more red-like color, not mostly green? 那么,如何更改此功能以使颜色可以均匀分布,从而可以生成更多的红色,而不是绿色?

We can use the function 我们可以使用功能

colorQuantile

From the library leaflet colorNumeric . 从库传单colorNumeric It maps continuous numeric data via the quantile function. 它通过分位数功能映射连续的数值数据。

colorQuantile(palette, domain, n = 4, probs = seq(0, 1, length.out = n
+ 1), na.color = "#808080", alpha = FALSE, reverse = FALSE,
right = FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM