更改 R 中 ggplot geom_polygon 的颜色方案

发布于 2024-11-07 07:01:20 字数 1927 浏览 5 评论 0原文

我正在使用地图库和 ggplot 的 geom_polygon 创建地图。我只是想将默认的蓝色、红色、紫色配色方案更改为其他颜色。我对 ggplot 非常陌生,所以如果我没有使用正确的数据类型,请原谅。这是我使用的数据的样子:

> head(m)
region      long      lat group order subregion Group.1 debt.to.income.ratio.mean    ratio total
17 alabama -87.46201 30.38968     1     1      <NA> alabama                   12.4059   20.51282    39
18 alabama -87.48493 30.37249     1     2      <NA> alabama                   12.4059 20.51282    39
19 alabama -87.52503 30.37249     1     3      <NA> alabama                   12.4059 20.51282    39
20 alabama -87.53076 30.33239     1     4      <NA> alabama                   12.4059 20.51282    39
21 alabama -87.57087 30.32665     1     5      <NA> alabama                   12.4059 20.51282    39
22 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 total
alabama       alabama                  12.40590 20.51282    alabama    39
alaska         alaska                  11.05333 33.33333     alaska     6
arizona       arizona                  11.62867 25.55556    arizona    90
arkansas     arkansas                  11.90300  5.00000   arkansas    20
california california                  11.00183 32.59587 california   678
colorado     colorado                  11.55424 30.43478   colorado    92

这是代码:

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

我已经尝试了以下及更多内容:

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

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 total
17 alabama -87.46201 30.38968     1     1      <NA> alabama                   12.4059   20.51282    39
18 alabama -87.48493 30.37249     1     2      <NA> alabama                   12.4059 20.51282    39
19 alabama -87.52503 30.37249     1     3      <NA> alabama                   12.4059 20.51282    39
20 alabama -87.53076 30.33239     1     4      <NA> alabama                   12.4059 20.51282    39
21 alabama -87.57087 30.32665     1     5      <NA> alabama                   12.4059 20.51282    39
22 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 total
alabama       alabama                  12.40590 20.51282    alabama    39
alaska         alaska                  11.05333 33.33333     alaska     6
arizona       arizona                  11.62867 25.55556    arizona    90
arkansas     arkansas                  11.90300  5.00000   arkansas    20
california california                  11.00183 32.59587 california   678
colorado     colorado                  11.55424 30.43478   colorado    92

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2024-11-14 07:01:20

问题是您正在使用色标,但在绘图中使用填充美学。您可以使用 scale_fill_gradient() 表示两种颜色,使用 scale_fill_gradient2() 表示三种颜色:

p + scale_fill_gradient(low = "pink", high = "green") #UGLY COLORS!!!

我在使用 scale_fill_brewer() 时遇到了问题,抱怨连续的颜色当需要离散变量时提供变量。一个简单的解决方法是使用 cut() 创建离散的垃圾箱,然后将其用作填充美学:

m$breaks <- cut(m$ratio, 5) #Change to number of bins you want

p <- qplot(long, lat, data = m, group = group, fill = breaks, geom = "polygon")
p + scale_fill_brewer(palette = "Blues")

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 want

p <- qplot(long, lat, data = m, group = group, fill = breaks, geom = "polygon")
p + scale_fill_brewer(palette = "Blues")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文