我相信这个问题与之前询问的类似问题略有不同,因为使用了scale_fill_brewer(
。我工作在一个地区分布与此类似https://gist.github.com/233134更改文字/标签ggplot图例
,看起来像这样:
而像传说:
我喜欢它,但要将图例上的标签从切割外观标签(例如(2,4))更改为更友好的内容,如'2%至4%'或'2% - 4%'。我在其他地方看过它;很容易更改刻度__中的标签,如here所示。我似乎无法弄清楚标签=参数的放置位置。我当然可以重新编码choropleth$rate_d
,但这似乎是低效的。我应该在哪里放置参数labels=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")
预先感谢您的协助。
编辑:使用迪文的方法(应该已经张贴了这个错误,因为这是我撞到前)
> 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
你太快了。我看到了错误并修复了它。我得到了一个稍微更多的信息:“错误:标签和中断必须是相同的长度” – 2012-02-17 03:28:45