本文介绍了有没有一种操纵ggplot刻度和标签的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 然而,我找到了一个很好的方法来创建合理的中断和标签。在包含许多方面和可能是 formatter = 语句的情节中,标签往往会变得太密集和叠印,例如在这张图片中: df 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 = 参数。 然而,我正在处理调查数据时有很多问题和十几个交叉破坏,所以需要找到一种方法来做到这一点 有没有一种方法可以告诉 ggplot 来自动计算中断和标签,但只有更少的在最小值,最大值和零点? 编辑:理想情况下,我不想指定最小和最大点,但不知何故点击内置的ggplot标度训练,并使用默认的计算尺度限制。解决方案如 min()和 max()来调用ggplot来动态指定中断。这听起来像你将要在各种各样的数据中应用这一点,所以你可能想考虑把它推广到一个函数中,并与格式相混淆,但是这种方法应该可以工作: ggplot(df,aes(x = x))+ geom_bar(binwidth = 0.5)+ facet_grid(〜fac)+ scale_x_continuous(broken = c(min(df $ x),0,max(df $ x)),labels = c(paste(100 * round(min(df $ x),2),% ,粘贴(0,%,sep =),粘贴(100 * round(max(df $ x),2),%,sep =))) 或者用 opts(axis.text)旋转x轴文本.x = theme_text(angle = 90,hjust = 0))生成如下所示的内容: $ b 更新 在最新版本的 ggplot2 中,中断和标签参数为 scale_x_continuous acc ept函数,所以可以做如下的事情: myBreaks< - function(x){ breaks < - c(min(x),median(x),max(x))名称(休息)< - attr(休息,标签)休息} 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 generally does a good job of creating sensible breaks and labels in scales.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")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.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?EDIT: 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. 解决方案 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 = "")) )or rotate the x-axis text with opts(axis.text.x = theme_text(angle = 90, hjust = 0)) to produce something like:UpdateIn 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刻度和标签的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-19 00:44