Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set limits with coord_trans

Tags:

r

ggplot2

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')

enter image description here

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.

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)))

enter image description here

like image 679
John Colby Avatar asked Jan 19 '12 18:01

John Colby


1 Answers

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()

enter image description here

like image 168
bdemarest Avatar answered Oct 26 '22 10:10

bdemarest