有没有办法操纵 ggplot 比例尺中断和标签? [英] Is there a way of manipulating ggplot scale breaks and labels?

查看:20
本文介绍了有没有办法操纵 ggplot 比例尺中断和标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot 通常可以很好地在比例尺中创建合理的中断和标签.

ggplot generally does a good job of creating sensible breaks and labels in scales.

然而,我发现在有很多方面的情节中,也许还有一个 formatter= 语句,标签往往会变得太密集"和叠印,例如在这张图片中:

However, I find that in plot with many facets and perhaps a formatter= statement, the labels tend to get too "dense" and overprint, for example in this picture:

df <- data.frame(
        fac=rep(LETTERS[1:10], 100),
        x=rnorm(1000)
)

ggplot(df, aes(x=x)) + 
  geom_bar(binwidth=0.5) + 
  facet_grid(~fac) + 
  scale_x_continuous(formatter="percent")

我知道我可以通过向 scale_x_continuous 提供 breaks=scale= 参数来明确指定刻度的中断和标签.

I know that I can specify the breaks and labels of scales explicitly, by providing breaks= and scale= arguments to scale_x_continuous.

但是,我正在处理具有许多问题和十几个交叉点的调查数据,因此需要找到一种方法来自动执行此操作.

However, I am processing survey data with many questions and a dozen crossbreaks, so need to find a way to do this automatically.

有没有办法告诉 ggplot 自动计算中断和标签,但只有更少,比如在最小值、最大值和零点?

Is there a way of telling ggplot to calculate breaks and labels automatically, but just have fewer, say at the minimum, maximum and zero point?

理想情况下,我不想指定最小值和最大值点,而是以某种方式利用内置的 ggplot 尺度训练,并使用默认的计算尺度限制.>

Ideally, I don't want to specify the minimum and maximum points, but somehow tap into the built-in ggplot training of scales, and use the default calculated scale limits.

推荐答案

您可以在调用 ggplot 时传入诸如 min()max() 之类的参数动态指定中断.听起来您将在各种数据中应用此方法,因此您可能需要考虑将其概括为一个函数并弄乱格式,但这种方法应该有效:

You can pass in arguments such as min() and max() in your call to ggplot to dynamically specify the breaks. It sounds like you are going to be applying this across a wide variety of data so you may want to consider generalizing this into a function and messing with the formatting, but this approach should work:

ggplot(df, aes(x=x)) + 
  geom_bar(binwidth=0.5) + 
  facet_grid(~fac) + 
  scale_x_continuous(breaks = c(min(df$x), 0, max(df$x))
    , labels = c(paste( 100 * round(min(df$x),2), "%", sep = ""), paste(0, "%", sep = ""), paste( 100 * round(max(df$x),2), "%", sep = ""))
    )

或使用 opts(axis.text.x = theme_text(angle = 90, hjust = 0)) 旋转 x 轴文本以产生类似:

or rotate the x-axis text with opts(axis.text.x = theme_text(angle = 90, hjust = 0)) to produce something like:

更新

ggplot2 的最新版本中,scale_x_continuousbreakslabels 参数接受函数,因此可以执行以下操作:

In the latest version of ggplot2 the breaks and labels arguments to scale_x_continuous accept functions, so one can do something like the following:

myBreaks <- function(x){
    breaks <- c(min(x),median(x),max(x))
    names(breaks) <- attr(breaks,"labels")
    breaks
}

ggplot(df, aes(x=x)) + 
  geom_bar(binwidth=0.5) + 
  facet_grid(~fac) + 
  scale_x_continuous(breaks = myBreaks,labels = percent_format()) + 
  opts(axis.text.x = theme_text(angle = 90, hjust = 1,size = 5))

这篇关于有没有办法操纵 ggplot 比例尺中断和标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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