Reputation: 173
Im having some problems with the following code:
p3<-ggplot(data.frame(x=c(-10, 30),y=c(0,250)), aes(x))
p3<-p3+geom_abline(intercept = 44, slope = 0,lty=2)
p3<-p3+coord_cartesian(ylim=c(0,200))
p3<-p3 +stat_function(fun=function(x)12+(160)/(1+exp(-.759*(x-7.69))),color="yellow2",aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))), linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),size=1.5) +
scale_linetype_manual(values=c(1,3))+scale_x_continuous(limits=c(-5,25))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.ticks.y=element_blank(), axis.ticks.x=element_blank(),axis.text.x=element_blank(),axis.text.y=element_blank() ,plot.title = element_text(hjust = 0.5),legend.position = "none") +xlab('') +ylab("")
p3<-p3+stat_function(fun=function(x)9+(160)/(1+exp(-.759*(x-7.69))),size = 2,color="yellow",aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))), linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),size=1.5)+scale_linetype_manual(values=c(1,3))
p3<-p3+coord_trans(y = "log")
p4<-ggplot(data.frame(x=c(-10, 30),y=c(0,250)), aes(x))
p4<-p4+geom_abline(intercept = 44, slope = 0,lty=2)
p4<-p4+coord_cartesian(ylim=c(0,200))
p4<-p4 +stat_function(fun=function(x)12+(160)/(1+exp(-.759*(x-7.69))),color="coral4",aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))), linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),size=1.5) +
scale_linetype_manual(values=c(1,3))+scale_x_continuous(limits=c(-5,25))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),axis.ticks.y=element_blank(), axis.ticks.x=element_blank(),axis.text.x=element_blank(),axis.text.y=element_blank() ,plot.title = element_text(hjust = 0.5),legend.position = "none") +xlab('') +ylab("")
p4<-p4+stat_function(fun=function(x)12+(180)/(1+exp(-.759*(x-7.69))),size = 2,color="coral2",aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))), linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),size=1.5) + scale_linetype_manual(values=c(1,3))
p4<-p4+coord_trans(y = "log")
Namely, when I plot p3 and p4, I can't seem to get the scales to line up so that the dotted horiztonal line is at the same level in both plots. What am I doing wrong? It seems that one of the stat_functions is messing things up but I don't know how to fix it.
Thanks.
Upvotes: 0
Views: 550
Reputation: 35382
Each plot can only have one coordinate system.
So when you first add coord_cartesian(ylim=c(0,200))
and then coord_trans(....)
, the y limits you had set are lost.
A second problem is that you can't set the limits of a log scale to zero, since zero values are not represented on a log scale. Let's use 8 instead, since that seems to work nicely here.
p1 <- ggplot(data.frame(x=c(-10, 30),y=c(0,250)), aes(x)) +
geom_abline(intercept = 44, slope = 0,lty=2) +
stat_function(fun = function(x) 12 + (160) / (1 + exp(-.759 * (x-7.69))),color="yellow2",
aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))),
linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),
size=1.5) +
stat_function(fun=function(x)9+(160)/(1+exp(-.759*(x-7.69))),color="yellow",
aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))),
linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),size=1.5) +
scale_x_continuous(limits=c(-5,25)) +
coord_trans(y = "log", limy = c(8, 200)) +
theme(legend.position = 'none')
p2 <- ggplot(data.frame(x=c(-10, 30),y=c(0,250)), aes(x)) +
geom_abline(intercept = 44, slope = 0,lty=2)+
stat_function(fun=function(x)12+(160)/(1+exp(-.759*(x-7.69))),color="coral4",
aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))),
linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),size=1.5) +
scale_linetype_manual(values=c(1,3))+scale_x_continuous(limits=c(-5,25)) +
stat_function(fun=function(x)12+(180)/(1+exp(-.759*(x-7.69))),color="coral2",
aes(group=factor(c(0,cumsum(diff(..y.. >= 44) != 0))),
linetype=factor(c(0,cumsum(diff(..y.. >= 44) != 0)))),size=1.5) +
coord_trans(y = "log", limy = c(8, 200)) +
theme(legend.position = 'none')
cowplot::plot_grid(p1, p2)
Also note that in your code, you are setting aesthetics multiple times, have duplicated scales, and just generally appalling readability. You make fixing anything really hard for yourself.
Upvotes: 2