无法使用coord_trans设置限制 [英] Can't set limits with coord_trans

查看:143
本文介绍了无法使用coord_trans设置限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些显示几何关系的数据,但有异常值。例如:

I have some data that show a geometric relationship, but have outliers. For example:

x = seq(0.1, 1, 0.01)
dat = data.frame(x=x, y=10^x)
dat[50:60, 2] = 10

qplot(x, y, data=dat, geom='line')

我想使用日志转换来绘制它,同时放大部分数据。我知道我可以用 coord_trans(y ='log10')来完成第一部分,或者第二部分用 coord_cartesian(ylim = c(2 ,8)),但我无法将它们组合。此外,我需要保留这些点,所以简单地用 scale_y_continuous(limits = c(2,8))剪切它们对我来说不起作用。

I'd like to plot this using a log transform and while zoomed in on part of the data. I know that I can do the first part with coord_trans(y='log10'), or the second part with coord_cartesian(ylim=c(2,8)), but I haven't been able to combine them. Also, I need to keep these points around, so simply clipping them with scale_y_continuous(limits=c(2,8)) won't work for me.

有没有办法做到这一点,而不必诉诸以下可怕的黑客?也许是一种无证的方式来将限制传递给 coord_trans

Is there a way to accomplish this without having to resort to the following terrible hack? Maybe an undocumented way to pass limits to coord_trans?

pow10 <- function(x) as.character(10^x)

qplot(x, log10(y), data=dat, geom='line') +
  scale_y_continuous(breaks=log10(seq(2,8,2)), formatter='pow10') +
  coord_cartesian(ylim=log10(c(2,8)))

推荐答案

这可能是一个稍微简单的解决方法:

This may be a slightly simpler work-around:

library(ggplot2)

x = seq(0.1, 1, 0.01)
dat = data.frame(x=x, y=10^x)
dat[50:60, 2] = 10

plot_1 = ggplot(dat, aes(x=x, y=y)) +
         geom_line() +
         coord_cartesian(ylim=c(2, 8)) +
         scale_y_log10(breaks=c(2, 4, 6, 8), labels=c("2", "4", "6", "8"))

png("plot_1.png")
print(plot_1)
dev.off()

这篇关于无法使用coord_trans设置限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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