强制ggplot2 y轴标签仅为整数,并给予适当的休息时间 [英] forcing ggplot2 y-axis label to be of integer only, and give proper breaks

查看:448
本文介绍了强制ggplot2 y轴标签仅为整数,并给予适当的休息时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为离散数据绘制条形图,ggplot默认为我调整y轴,但给了我0.5个间隔的y轴标签,我不喜欢它。我试过 scale_y_discrete ,但是对于每个离散值都给出了y轴断点,这也不好。



我可以强制y轴中断仅由整数组成,并为每个方面给予正确的中断吗?

示例脚本如下:

  set.seed(1)
chart.data< - data.frame(x = rep(LETTERS [1:10],3),
y = c(sample(0:10,10,replace = TRUE),
sample(0:100,10 ,
sample(0:1000,10,replace = TRUE)),
group = sort(rep(1:3,10)))
图表< - ggplot(data = chart.data,aes(x = x,y = y))
图表< - chart + geom_bar(stat =identity)
图表< - chart + scale_y_discrete()
图< - chart + facet_wrap(facets =〜group,nrow = 1,scale =free_y)



更新#1



由于该帖子被视为可能重复,因此该脚本被改进以显示更复杂的场景。 b $ b

解决方案

首先,因为你的y数据是c连续的,你应该使用 scale_y_continuous()。在这个函数中,你可以添加参数 breaks = pretty_breaks()(add library scales 来使用函数 pretty_breaks())。如果你没有在 pretty_breaks()内提供任何数字,那么在这种情况下,你将在y轴上得到整数。您可以设置要显示的中断点数量,例如, pretty_breaks(4),但对于第一个有0-10范围的分面,它仍然只显示整数值,而
$ b

  library(比例)
ggplot(数据=图表) .data,aes(x = x,y = y))+
geom_bar(stat =identity)+
facet_wrap(facets =〜group,nrow = 1,scale =free_y)+
scale_y_continuous(breaks = pretty_breaks())


I am drawing barchart for discrete data and ggplot by default adjust the y-axis for me, but gives me y-axis label with breaks at 0.5 interval which I don't like it. I tried scale_y_discrete but y-axis breaks is given for every discrete value, which is not good also.

Can I force the y-axis break to be composed of integer only, and give proper breaks for each of the facet?

Sample script is as below:

set.seed(1)
chart.data <- data.frame(x=rep(LETTERS[1:10],3),
                         y=c(sample(0:10,10,replace=TRUE),
                             sample(0:100,10,replace=TRUE),
                             sample(0:1000,10,replace=TRUE)),
                         group=sort(rep(1:3,10)))
chart <- ggplot(data=chart.data,aes(x=x,y=y))
chart <- chart + geom_bar(stat="identity")
chart <- chart + scale_y_discrete()
chart <- chart + facet_wrap(facets=~group,nrow=1,scale="free_y")

Update #1

Since the post is being considered as possible duplicate, the script is refined to show a more complicated scenario.

解决方案

First, as your y data are continuous you should use scale_y_continuous(). In this function you can add argument breaks= pretty_breaks() (add library scales to use function pretty_breaks()). If you don't provide any number inside pretty_breaks() then in this case you will get integer numbers on y axis. You can set number of breaks to display, for example, pretty_breaks(4) but for the first facet where you have range 0-10 it will still display only integer values and the number of breaks will be larger to get "nice" numbers.

library(scales)
ggplot(data=chart.data,aes(x=x,y=y)) + 
  geom_bar(stat="identity") +  
  facet_wrap(facets=~group,nrow=1,scale="free_y")+
  scale_y_continuous(breaks= pretty_breaks())

这篇关于强制ggplot2 y轴标签仅为整数,并给予适当的休息时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆