R自定义色标进行绘图 [英] R Custom Color Scale for Plotting

查看:55
本文介绍了R自定义色标进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义标尺,以更好地显示我拥有的数据.对于第一个图,数据相对均匀,介于0到3之间.但是,对于我的第二个数据集,大多数数据小于100,大约90%的数据小于1000.)

具有我要绘制的值的列称为"data1",并且这些值是介​​于0到10000之间的整数.

我尝试在

解决方案

您快到了.以下带注释的代码中的解释:

 #add include.lowest = TRUE,否则0值将变为NAmap.df $ brks<-剪切(map.df $ data1,休息= c(0,1,10,25,100,250,1000,10000),标签= c("0","1-10","11-25","26-100","101-250","251-1000","1001-10000"),include.lowest = TRUE)#使用scale_fill_brewer而不是scale_fill_gradientn;它期望#一个scale_fill_gradientn期望的分类变量(例如brks)#连续变量(例如data1).#另外,我建议将图例放在侧面,作为垂直堆叠#框,类似于颜色栏.#theme_void可用于删除所有背景,轴标签等.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()+主题(legend.position =正确") 

I am attempting to create a custom scale that will better display the data I have. For the first plot, the data was relatively uniform, falling in between 0 and 3. However, for my second dataset, the majority of the data is less than 100, and approximately 90% of the data is under 1000. (plots at bottom)

The column with the values I am trying to plot is named 'data1', and the values are whole numbers ranging from 0-10000.

I tried applying the code on https://gis.stackexchange.com/questions/178597/how-to-create-a-continuous-scale-with-distinct-custom-color-and-value-breaks-wit by inserting

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"))

where the commented line is and replacing the fill argument with brks in the ggplot function call, but I get the following error: Error: Discrete value supplied to continuous scale.

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())

Does anyone have advice on how I can create a custom scale? Thanks in advance!

解决方案

You are almost there. Explanations in annotated code below:

# 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")

这篇关于R自定义色标进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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