ggplot multiple layers from different dataframes with y-axis scaling mismatch?

Hi
I created a ggplot with geom_point and two geom_line with each series from a different dataframe.

The x-coordinates for the points and lines do not line up as the points are random where as the lines are based on exponential type functions and used regularly spaced x-coords. Hence its not obvious how to merge them into one dataframe.

I assumed that the y values would share the same y-axis but recently I was asked to zoom into the plot and this assumption now appears wrong.

I tried using coord_cartesian and also pre-filtering the data but can see that the lines are crossing the points at very different locations. Thus in the original plot, a point my have been below the line but in the zoomed plot, the point now lies above the line.

So the zoomed plot looks radically different to the original. So presumably my layers are using different y-axis scales and the plots are wrong.

I think I have misunderstood how to combine layers in ggplot and so please suggest solution.

Here are my reprex. In the original the pit ioint lies above the line but in the zoomed version it is below. I have used spline. Perhaps the issue in this case is just the interpolation but in my actual example which is too complex to reproduce, this is not the case and there I think different y-axis scaling has been used.

require(ggplot2)
require(magrittr)
require(dplyr)

icexp=10
slexp=8
spdexp=1:8*.1-.7
tibexp<-tibble(spdexp,icexp,slexp)
tibexp%<>%dplyr::mutate(expexp=exp(slexp*spdexp+icexp)/1000)%>%
  dplyr::select(-slexp,-icexp)
tibexp<-as.data.frame(spline(tibexp$spdexp,tibexp$expexp))

tibpnt<-tibble(x=-.05,y=15)

ggplot() + 
geom_point(data=tibpnt, aes(x=x, y=y),shape=21, alpha=1, size =10) +
theme_bw() +
geom_line(data=tibexp,aes(x=x,y=y),color="red",size=4,alpha=.3)+
theme(text = element_text(size = 45)) 

require(ggplot2)
require(magrittr)
require(dplyr)

icexp=10
slexp=8
spdexp=1:2*.1-.2
tibexp<-tibble(spdexp,icexp,slexp)
tibexp%<>%dplyr::mutate(expexp=exp(slexp*spdexp+icexp)/1000)%>%
  dplyr::select(-slexp,-icexp)
tibexp<-as.data.frame(spline(tibexp$spdexp,tibexp$expexp))

tibpnt<-tibble(x=-.05,y=15)

ggplot() + 
geom_point(data=tibpnt, aes(x=x, y=y),shape=21, alpha=1, size =10) +
theme_bw() +
geom_line(data=tibexp,aes(x=x,y=y),color="red",size=4,alpha=.3)+
theme(text = element_text(size = 45)) 

Perhaps I am missing your point but I do not see any problem with the y scale in your example. I modified the code to put everything on one plot and show both the data points and the fit lines. With 8 data points in tibexp the fit is a curved line bowing downwards. With 2 data points in tibexp2, the fit is a straight line which is always above the first spline fit except at the end points where they meet. I have plotted the data twice, once with the full scale and a second time adjusting the scale with coord_cartesian.

require(ggplot2)
#> Loading required package: ggplot2
require(magrittr)
#> Loading required package: magrittr
require(dplyr)
#> Loading required package: dplyr

icexp=10
slexp=8
spdexp = 1:8 * .1 - .7
tibexp<-tibble(spdexp,icexp,slexp)
tibexp %<>% mutate(expexp = exp(slexp * spdexp + icexp)/1000) %>%
  select(-slexp,-icexp)
Spl <- as.data.frame(spline(tibexp$spdexp,tibexp$expexp))

tibpnt<-tibble(x=-.05,y=15)

spdexp = 1:2 * .1 - .2
tibexp2 <- tibble(spdexp,icexp,slexp)
tibexp2 %<>% mutate(expexp = exp(slexp * spdexp + icexp)/1000) %>%
  select(-slexp,-icexp)
Spl2<- as.data.frame(spline(tibexp2$spdexp,tibexp2$expexp))

ggplot() + 
  geom_point(data=tibpnt, aes(x=x, y=y),shape=21, alpha=1, size =10) +
  theme_bw() +
  geom_point(data=tibexp,aes(x=spdexp,y=expexp),color="red",size=4,alpha=.3) +
  geom_point(data=tibexp2,aes(x=spdexp,y=expexp),color="blue",size=4,alpha=.3) +
  geom_line(data=Spl,aes(x=x,y=y),color="red",size=4,alpha=.3) +
  geom_line(data=Spl2,aes(x=x,y=y),color="blue",size=4,alpha=.3) 


ggplot() + 
  geom_point(data=tibpnt, aes(x=x, y=y),shape=21, alpha=1, size =10) +
  theme_bw() +
  geom_point(data=tibexp,aes(x=spdexp,y=expexp),color="red",size=4,alpha=.3) +
  geom_point(data=tibexp2,aes(x=spdexp,y=expexp),color="blue",size=4,alpha=.3) +
  geom_line(data=Spl,aes(x=x,y=y),color="red",size=4,alpha=.3) +
  geom_line(data=Spl2,aes(x=x,y=y),color="blue",size=4,alpha=.3) +
  coord_cartesian(xlim = c(-0.1, 0), ylim = c(9, 23))

Created on 2019-07-06 by the reprex package (v0.2.1)

Many thanks. I accept that it is an issue to do with interpolation.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.