在facet中重叠的y尺度(scale =“free”) [英] overlapping y-scales in facet (scale="free")

查看:195
本文介绍了在facet中重叠的y尺度(scale =“free”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几周里我一直在学习ggplot。一般来说,我正在完成任务(尽管缓慢),但现在我卡住了。我创建了以下小平面图: http://dl.dropbox.com/u/7752237 /example_bad_y_scales.pdf



分面是由

  pl <-pl + facet_wrap(〜sci_name,ncol = 1,scale =free)

问题:y尺度上的数字看起来不太好,特别是从0-70(数字重叠)的尺度。
我想以某种方式改变y尺度上的休息数(假设只有1或2个休息时间)。有人可能有一个想法如何做到这一点?任何帮助将非常感激。 :)



PS:我没有包含一个最简单的例子,因为我认为解决这个特定问题没有多大帮助。



在Kohskes回答后编辑:

您好Kohske,
哇,这真是一个非常快速的答案,谢谢!但是,我认为它不适用于多面的情节。看看

  p < -  ggplot(mtcars,aes(wt,mpg))
p < - p + geom_point()
p <-p + facet_wrap(〜gear,ncol = 1,scale =free)

在y尺度上,它在中间情节中有3个中断,在下方情节中有8个中断...不是很一致(但至少在我的例子中不重叠)。

  p2 < -  p + scale_y_continuous(breaks = c(15,30),minor_breaks = c(10,20,25))

也不是很好:两个较低点的蜱虫,只有一个在中间和上面的情节。
当比例差异大于mtcars时,结果会更不令人满意。任何其他想法? ;)

在Kohskes编辑之后编辑:



我看不到如何实现这个。在google上搜索ggplot和input_break只产生了10个结果,他们都没有帮助。
我试过

pre $ lt; code> p < - ggplot(mtcars,aes(wt,mpg))
p< ; - p + geom_point()
p <-p + facet_wrap(〜gear,ncol = 1,scale =free)
p $ input_breaks <-function(。,range){
(范围,n = 3)
}
print(p)

但是,我无法在图中看到任何效果(尝试n = 1,3,15)。你能描述一下如何在mtcars例子中实现这个吗?

解决方案

p <-ggplot(mtcars,aes(wt,mpg))
p < - p + geom_point()

  dev.new(height = 1)
print(p)
dev。新(高度= 1)
p <-p + scale_y_continuous(休息= c(15,30),minor_breaks = c(10,20,25))
print(p)

诀窍是scale_y_continuous,您可以在其中指定分行和小分行。



编辑:

您可能无法分别为每个方面指定中断。
一种解决方法是通过以下方式来控制休息的美丽:

$ $ $ $ $ $ $ Trans $ input_breaks< -function(。,range ){
pretty(range,n = 3)
}
print(p)


$

再次编辑:

改变n = 3会产生不同的可爱。这里是完整的例子:

  library(ggplot2)
p < - ggplot(mtcars,aes(wt,mpg) )+ geom_point()
Trans $ input_breaks< -function(。,range){
pretty(range,n = 100)
}
print(p)

在这种情况下,您可能会看到上百个勾号。
通过改变n = 100,您可以自定义它。



注意这有副作用。之后的所有图表具有相同数量的刻度,并且x和y轴具有相同数量的刻度。

I've been learning ggplot in the last few weeks. Generally, I'm getting things done (slowly though), but now I'm stuck. I created the following facetted plot: http://dl.dropbox.com/u/7752237/example_bad_y_scales.pdf

Faceting is done by

pl <- pl + facet_wrap(~sci_name,ncol=1,scale="free")

The Problem: Numbers on the y-scale don't look good, especially the scales that go from 0-70 (numbers overlapping). I'd like the somehow change the number of breaks on the y-scale (to let's say just 1 or 2 breaks). Does anybody maybe have an idea how to do that? Any help would be very much appreciated. :)

PS: I didn't include a minimal example because I think it wouldn't help much to solve that specific problem.

Edit after Kohskes answer:

Hi Kohske, Wow, that was a really fast answer, thanks! However, I think it doesn't work well with facetted plots. Look at

p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
p <- p + facet_wrap(~gear,ncol=1,scale="free")

On the y-scale, it gives 3 breaks in the middle plot and 8 breaks in the lower plot… not very consistent (but at least not overlapping as in my example).

p2 <- p + scale_y_continuous(breaks=c(15,30),minor_breaks=c(10,20,25))

isn't really good neither: two major ticks on lower plots, only one in middle and upper plot. When having scales with bigger differences than in mtcars, the result would be even less satisfying. Any other ideas? ;)

Edit after Kohskes edit:

Hi, I can't see how to implement this. Searching for ggplot and input_break on google yielded only 10 results, none of them did help. I tried

p <- ggplot(mtcars, aes(wt, mpg))
p <- p + geom_point()
p <- p + facet_wrap(~gear,ncol=1,scale="free")
p$input_breaks<-function(., range) {
    pretty(range, n=3)
}
print(p)

However, I can't see any effects in the graph (tried for n=1, 3, 15). Could you describe how to implement this on the mtcars example? Thanks!

解决方案

p <- ggplot(mtcars, aes(wt, mpg)) p <- p + geom_point()

dev.new(height=1)
print(p)
dev.new(height=1)
p <- p + scale_y_continuous(breaks=c(15,30),minor_breaks=c(10,20,25))
print(p)

the trick is scale_y_continuous and you can specify the breaks and minor breaks in it.

edited:

probably you cannot specify the breaks separately for each facet. one workaround is to control the prettiness of the breaks by:

Trans$input_breaks<-function(., range) {
    pretty(range, n=3)
}
print(p)

changing the "n=3" yields different prettiness.

edited again:

here is full example:

library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))+geom_point()
Trans$input_breaks<-function(., range) {
    pretty(range, n=100)
}
print(p)

in this case, probably you can see a hundred of ticks. by changing n=100, you can custom it.

note that this has side-effect. all plots after this has same number of ticks, and also x and y axis have the same number of ticks.

这篇关于在facet中重叠的y尺度(scale =“free”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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