6

我相信这个问题与之前在这里提出的类似问题略有不同,因为使用了scale_fill_brewer(. 我正在研究与此类似的等值线https://gist.github.com/233134

看起来像这样:

叶绿素

和传说一样:

传奇

我喜欢它,但想将图例上的标签从剪裁标签(即( 2 , 4 ]很容易更改 scale_... 内的标签,如此处所示我似乎无法弄清楚标签 = 参数的放置位置。我当然可以重新编码,但这似乎效率低下。我应该把论据?choropleth$rate_dlabels=c(A, B, C, D...)

这是一段感兴趣的代码(完整代码使用上面的链接)

choropleth$rate_d <- cut(choropleth$rate, breaks = c(seq(0, 10, by = 2), 35))

# Once you have the data in the right format, recreating the plot is straight
# forward.

ggplot(choropleth, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
  geom_polygon(data = state_df, colour = "white", fill = NA) +
  scale_fill_brewer(pal = "PuRd")

预先感谢您的协助。

编辑:使用 DWin 的方法(应该发布这个错误,因为这是我之前遇到的)

> ggplot(choropleth, aes(long, lat, group = group)) +
+   geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
+   geom_polygon(data = state_df, colour = "white", fill = NA) +
+   scale_fill_brewer(pal = "PuRd", labels = lev4)
Error: Labels can only be specified in conjunction with breaks
4

1 回答 1

10

除了添加关卡的修改版本外,您还需要将“breaks”参数设置为scale_fill_brewer

lev = levels(rate_d)  # used (2, 4] as test case
 lev2 <- gsub("\\,", "% to ", lev)
 lev3 <- gsub("\\]$", "%", lev2)
 lev3
[1] "(2% to 4%"
lev4 <- gsub("\\(|\\)", "", lev3)
 lev4
[1] "2% to 4%"

ggplot(choropleth, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
  geom_polygon(data = state_df, colour = "white", fill = NA) +
  scale_fill_brewer(pal = "PuRd", labels = lev4, , breaks=seq(0, 10, by = 2) )
于 2012-02-17T03:15:22.983 回答