【问题标题】:R Custom Color Scale for Plotting用于绘图的 R 自定义色标
【发布时间】:2018-10-02 04:43:43
【问题描述】:

我正在尝试创建一个自定义比例,以更好地显示我拥有的数据。对于第一个图,数据比较均匀,介于 0 和 3 之间。但是,对于我的第二个数据集,大部分数据小于 100,大约 90% 的数据在 1000 以下。(底部的图)

包含我要绘制的值的列名为“data1”,值是从 0 到 10000 的整数。

我尝试通过插入将代码应用到https://gis.stackexchange.com/questions/178597/how-to-create-a-continuous-scale-with-distinct-custom-color-and-value-breaks-wit

map.df$brks <- cut(map.df$data1, 
                 breaks=c(0, 1, 10, 25, 100, 250, 1000, 10000), 
                 labels=c("0", "1 - 10", "11 - 25", "26 - 100", 
                          "101 - 250", "251 - 1000", "1001 - 10000"))

注释行在哪里,并在 ggplot 函数调用中用 brks 替换填充参数,但我收到以下错误:错误:提供给连续刻度的离散值

stateNames <- c("alabama", "alabama", "arizona", "arkansas", "california", 
                "colorado", "connecticut", "florida", "georgia")
countyNames <- c("autauga", "blount", "apache", "bradley", "orange", 
                 "boulder", "new haven", "flagler", "turner")
dataCol <- c(0, 5, 15, 50, 150, 1000, 5000, 249, 30)

library(ggplot2)
map.county <- map_data('county')
counties   <- unique(map.county[,5:6])
data_map <- merge(counties, data.frame(region=stateNames, 
                                     subregion=countyNames, 
                                     data1= dataCol),
                  by=c("region","subregion"), all.x=T, all.y=F
)

data_map$data1[which(is.na(data_map$data1))] <- fillValue
for(i in 1:length(data_map$data1)) {
  if(is.na(data_map$data1[i])) {
    data_map$data1[i] <- 0
  }
}
library(data.table)
map.county <- data.table(map_data('county'))
setkey(map.county,region,subregion)
data_map <- data.table(data_map)
setkey(data_map,region,subregion)
map.df <- map.county[data_map]

############################################################# 

ggplot(map.df, aes(x=long, y=lat, group=group, fill=data1)) + 
  scale_fill_gradientn("",colours=brewer.pal(9,"YlOrRd"))+
  geom_polygon(colour = borderColor, size = 0.001) +
  coord_map()  +
  expand_limits(x = map.county$long, y = map.county$lat) +
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") +
  labs(fill=dataName)  +
  theme(legend.position = "bottom", panel.background = element_blank())

有人对我如何创建自定义比例有建议吗?提前致谢!

【问题讨论】:

  • 您能否简化您的问题并提供我们可以使用的示例数据集,并阐明您所说的“自定义色标”是什么意思。它给你的那个有什么问题?
  • 我很抱歉不清楚。当我说“自定义颜色比例”时,我的意思是能够改变比例,使颜色不会以恒定的速率变化。如果您在上面看到,每 2000 个单位都有均匀间隔的刻度线。我希望能够用我自己的值替换这些数字,例如上面第一行代码所示的“0、1、10、25、100、250、1000、10000”。
  • 我已经用一个小样本数据集更新了这个问题

标签: r ggplot2 maps


【解决方案1】:

你快到了。以下注释代码中的解释:

# add include.lowest = TRUE, otherwise the 0 values will become NAs
map.df$brks <- cut(map.df$data1, 
                   breaks = c(0, 1, 10, 25, 100, 250, 1000, 10000), 
                   labels = c("0", "1 - 10", "11 - 25", "26 - 100", 
                            "101 - 250", "251 - 1000", "1001 - 10000"),
                   include.lowest = TRUE)

# use scale_fill_brewer rather than scale_fill_gradientn; it expects
# a categorical variable (e.g. brks) while scale_fill_gradientn expects
# a continuous variable (e.g. data1).
# also, I suggest placing the legend at the side, as a vertical stack of 
# boxes, which resembles a color bar.
# theme_void can be used to remove all backgrounds, axis labels, etc.
ggplot(map.df, aes(x=long, y=lat, group=group, fill=brks)) + 
  geom_polygon(colour = "grey", size = 0.001) +
  coord_map()  +
  expand_limits(x = map.county$long, y = map.county$lat) +
  scale_fill_brewer(palette = "YlOrRd") +
  theme_void() +
  theme(legend.position = "right")

【讨论】:

    猜你喜欢
    • 2023-03-02
    • 2022-01-20
    • 1970-01-01
    • 2021-06-22
    • 2016-03-22
    • 2016-12-10
    • 2014-01-06
    • 1970-01-01
    • 2021-02-17
    相关资源
    最近更新 更多