在ggplot中设置中断和标签 [英] setting breaks and labels in ggplot
问题描述
我已经将Shiny应用程序放在一起(如下图),显示了一个choropleth,除了传说中的科学记号外,这看起来不错。
我希望将这些值读为:'$ 5,000,000'和'$ 4,000,000'等等。
代码归结起来to
g <-choroplethr(DF,lod =state,num_buckets = 1)
g < - g + scale_fill_gradient(high =springgreen4,low =grey90,name =Sum)
我试过这个:
g <-q + scale_fill_gradient(high =springgreen4,low =grey90,name =Sum,labels = c(5,000,000,4,000,000,3,000,000,2,000,000,1,000,000))
但我收到了错误信息,中断和标签的长度不同 我不确定我可以在我处理地图时在x轴上指定断点?我如何创建符合我想要包含的标签的休息时间?感谢。
如果您将参数 labels =
添加到 scale_fill_gradient()
那么你还应该添加参数 breaks =
,它与你的标签长度相同。
$ b
+ scale_fill_gradient(high =springgreen4,low =grey90,name =Sum,
pre>
labels = c(5,000,000 ,4,000,000,3,000,000,2,000,000,1,000,000),
break = c(5000000,4000000,3000000,2000000,1000000))
在这种情况下,更好的解决方案是只使用
格式的
库。这会自动添加美元符号和逗号。dollar
格式>缩放
library(比例)
+ scale_fille_gradient(labels = dollar)
I have put together the Shiny app (below), showing a choropleth, which looks good except for the scientific notation on the legend.
I would like the values to read: '$5,000,000', and '$4,000,000', etc, etc.
The code boils down to
g <-choroplethr(DF, lod="state", num_buckets = 1) g <- g + scale_fill_gradient(high = "springgreen4", low= "grey90", name="Sum")
I attempted this:
g <- q + scale_fill_gradient(high = "springgreen4", low= "grey90", name="Sum", labels = c("5,000,000", "4,000,000", "3,000,000", "2,000,000", "1,000,000"))
But I got the error, Breaks and labels are different lengths
I'm not sure how I can specify breaks on the x axis when I'm dealing with a map? How can I create breaks that work with the labels I want to include? Thanks.
解决方案If you are adding argument
labels=
toscale_fill_gradient()
then you should also add argumentbreaks=
that is the same lenghts as your labels.+ scale_fill_gradient(high = "springgreen4", low= "grey90", name="Sum", labels = c("5,000,000", "4,000,000", "3,000,000", "2,000,000", "1,000,000"), breaks = c(5000000, 4000000, 3000000, 2000000, 1000000))
In this case a better solution would be to just use the
dollar
format from thescales
library. That will automatically add dollar signs and commas.library(scales) + scale_fille_gradient(labels=dollar)
这篇关于在ggplot中设置中断和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!