本文介绍了在R中更改ggplot geom_polygon的配色方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用maps library和ggplot的geom_polygon创建一个地图。我只想将默认的蓝色,红色,紫色方案更改为其他的。我非常新ggplot,所以请原谅,如果我只是不使用正确的数据类型。以下是我使用的数据如下: >头(m)区域长拉组合订单子区域1债务.to.income.ratio.mean比率总计 17阿拉巴马-87.46201 30.38968 1 1< NA>阿拉巴马州12.4059 20.51282 39 18阿拉巴马-87.48493 30.37249 1 2< NA>阿拉巴马州12.4059 20.51282 39 19阿拉巴马-87.52503 30.37249 1 3< NA>阿拉巴马州12.4059 20.51282 39 20阿拉巴马-87.53076 30.33239 1 4< NA>阿拉巴马州12.4059 20.51282 39 21阿拉巴马-87.57087 30.32665 1 5< NA>阿拉巴马州12.4059 20.51282 39 22阿拉巴马-87.58806 30.32665 1 6< NA>阿拉巴马州12.4059 20.51282 39 > (v)组1债务.to.income.ratio.mean比率区域总额阿拉巴马州阿拉巴马州12.40590 20.51282阿拉巴马州39 阿拉斯加州阿拉斯加州11.05333 33.33333阿拉斯加州6 亚利桑那州亚利桑那州11.62867 25.55556 arizona 90 阿肯色阿肯色州11.90300 5.00000阿肯色州20 加利福尼亚州加利福尼亚州11.00183 32.59587加州678 科罗拉多科罗拉多州11.55424 30.43478科罗拉多州92 这里是代码: library(ggplot2) library地图) 状态< - map_data(state)m< - merge(states,v,by =region)m< - m [ m $ order),] p 我已经尝试了以下和更多: cols p + scale_colour_manual(values = cols)p + scale_colour_brewer(palette =Set1)p + scale_color_manual((l),6=darkgreen,10=orange值= c(#CC6666,#9999CC)) 解决方案问题在于,您正在使用颜色比例,但在剧情中使用了填充审美。您可以使用两种颜色的 scale_fill_gradient()和 scale_fill_gradient2()三种颜色: p + scale_fill_gradient(low =pink,high =green)#UGLY COLORS !!! 我遇到问题与 scale_fill_brewer()抱怨当预期离散变量时提供的连续变量。一个简单的解决方案是使用 cut()创建离散的仓,然后将其用作填充审美: m $ break< - cut(m $ ratio,5)#更改您想要的bin数量 p< - qplot(long,lat,data = m ,group = group,fill = breaking,geom =polygon)p + scale_fill_brewer(palette =Blues) I'm creating a map using the maps library and ggplot's geom_polygon. I'd simply like to change the default blue, red, purple colour scheme to something else. I'm extremely new to ggplot so please forgive if I'm just not using the right data types. Here's what the data I'm using looks like:> head(m)region long lat group order subregion Group.1 debt.to.income.ratio.mean ratio total17 alabama -87.46201 30.38968 1 1 <NA> alabama 12.4059 20.51282 3918 alabama -87.48493 30.37249 1 2 <NA> alabama 12.4059 20.51282 3919 alabama -87.52503 30.37249 1 3 <NA> alabama 12.4059 20.51282 3920 alabama -87.53076 30.33239 1 4 <NA> alabama 12.4059 20.51282 3921 alabama -87.57087 30.32665 1 5 <NA> alabama 12.4059 20.51282 3922 alabama -87.58806 30.32665 1 6 <NA> alabama 12.4059 20.51282 39> head(v) Group.1 debt.to.income.ratio.mean ratio region totalalabama alabama 12.40590 20.51282 alabama 39alaska alaska 11.05333 33.33333 alaska 6arizona arizona 11.62867 25.55556 arizona 90arkansas arkansas 11.90300 5.00000 arkansas 20california california 11.00183 32.59587 california 678colorado colorado 11.55424 30.43478 colorado 92Here's the code:library(ggplot2)library(maps)states <- map_data("state")m <- merge(states, v, by="region")m <- m[order(m$order),]p<-qplot(long, lat, data=m, group=group, fill=ratio, geom="polygon")I've tried the below and more:cols <- c("8" = "red","4" = "blue","6" = "darkgreen", "10" = "orange")p + scale_colour_manual(values = cols)p + scale_colour_brewer(palette="Set1")p + scale_color_manual(values=c("#CC6666", "#9999CC")) 解决方案 The problem is that you are using a color scale but are using the fill aesthetic in the plot. You can use scale_fill_gradient() for two colors and scale_fill_gradient2() for three colors:p + scale_fill_gradient(low = "pink", high = "green") #UGLY COLORS!!!I was getting issues with scale_fill_brewer() complaining about a continuous variable supplied when a discrete variable was expected. One easy fix is to create discrete bins with cut() and then use that as the fill aesthetic:m$breaks <- cut(m$ratio, 5) #Change to number of bins you wantp <- qplot(long, lat, data = m, group = group, fill = breaks, geom = "polygon")p + scale_fill_brewer(palette = "Blues") 这篇关于在R中更改ggplot geom_polygon的配色方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 23:51