使用ggplot2中的scale_fill_brewer()函数为数据设置断点 [英] Setting breakpoints for data with scale_fill_brewer() function in ggplot2
问题描述
这个问题描述我遇到的问题。就像在答案中一样,我的解决方法是使用gtools quantcut()函数将我的数据预剪成bin:
更新:这个第一个例子实际上是($)
$ b
require(gtools)#需要的数量()
...
fill_factor< - quantcut(fill_continuous,q = seq(0,1,by = 0.25))
ggplot(mydata)+
aes(long ,lat,group = group,fill = fill_factor)+
geom_polygon()+
scale_fill_brewer(name =mybins,palette =PuOr)
不过,我觉得我应该可以跳过预先裁剪数据的步骤,并使用breaks选项执行此类操作:
ggplot(mydata)+
aes(long,lat,group = group,fill = fill_continuous)+
geom_polygon()+
scale_fill_brewer(names =mybins,palette =PuOr,breaks = quantile(fill_continuous))
但这不起作用。相反,我得到一个错误:
$ b
提供给离散scale_brewer的连续变量(复合分数)。
我误解了休息选项的用途吗?或者是破裂了?
正如Hadley解释的那样,break选项会移动ticks,但不会使数据连续。因此,按照问题中的第一个示例预先裁剪数据是使用scale_fill_brewer命令的正确方法。
I am creating a map (choropleth) as described on the ggplot2 wiki. Everything works like a charm, except that I am running into an issue mapping a continuous value to the polygon fill color via the scale_fill_brewer() function.
This question describes the problem I'm having. As in the answer, my workaround has been to pre-cut my data into bins using the gtools quantcut() function:
UPDATE: This first example is actually the right way to do this
require(gtools) # needed for quantcut()
...
fill_factor <- quantcut(fill_continuous, q=seq(0,1,by=0.25))
ggplot(mydata) +
aes(long,lat,group=group,fill=fill_factor) +
geom_polygon() +
scale_fill_brewer(name="mybins", palette="PuOr")
This works, however, I feel like I should be able to skip the step of pre-cutting my data and do something like this with the breaks option:
ggplot(mydata) +
aes(long,lat,group=group,fill=fill_continuous) +
geom_polygon() +
scale_fill_brewer(names="mybins", palette="PuOr", breaks=quantile(fill_continuous))
But this doesn't work. Instead I get an error something like:
Continuous variable (composite score) supplied to discrete scale_brewer.
Have I misunderstood the purpose of the "breaks" option? Or is breaks broken?
As Hadley explains, the breaks option moves the ticks, but does not make the data continuous. Therefore pre-cutting the data as per the first example in the question is the right way to use the scale_fill_brewer command.
这篇关于使用ggplot2中的scale_fill_brewer()函数为数据设置断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!