0

I used scale() function on my data to avoid high correlation when doing a mixed model. Now I want the original values to appear in my plot. So I reversed the scaling with x * attr(x, 'scaled:scale') + attr(x, 'scaled:center') and put the values in a new column of the dataframe that I use to plot. So as an example my data now looks something like this, where x is the real value and x.s the scaled value:

x <- sample(x=1:100, size = 50)
y <- sample(x=1:100, size = 50)
df <- as.data.frame(cbind(x,y))

df$x.s <- scale(df$x)

I want to plot this now with ggplot but show the values of x on x-axis and not the scaled values of x.s, so I did the following:

ggplot(df, aes(x = x.s, y = y))+
  geom_point()+
  scale_x_continuous(labels = df$x, breaks = df$x.s)+
  labs(x = "Canopy openness [%]", y = "Rarefied richness") + 
  theme_bw()

This works so far and the output looks something like this:

enter image description here

My problem now is, that i want the ticks on the x-Axis spread evenly which I would usually do with breaks=seq(0,100,10), but breaks is already defined to avoid error Error in f(..., self = self) : Breaks and labels are different lengths, now I can't figure out how to do this, any help would be appreciated!

If I use x on x-axis, in real Dataset my prediction regression with CI won´t fit anymore. Here are the Plots from my Dataset 1: with scaled values (x.s):

enter image description here

and 2: If I use x instead of x.s on x-axis

enter image description here

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
TreePete
  • 1
  • 4
  • You could define your own transform for the `trans` argument of `scale_x_continuous`, see `?scales::trans` for help. But the simple solution is to just map `x = x` inside `aes()`... it seems like you're making it much more complicated than it needs to be. – Gregor Thomas Nov 26 '19 at 14:10
  • Thanks for the info, the reason i am making it this complicated is that in the real data set I also plot a regression and confidence intervall of predictions into same plot, so if i justed used x=x they wouldn´t fit to the data anymore. How would I transform the trans argument? could you explain using the example? – TreePete Nov 26 '19 at 14:42
  • I don't really have time to write a tutorial on it, but you could check out other questions with "custom trans" in the ggplot tag, [like this one](https://stackoverflow.com/q/38333603/903061), or see the examples in the `scales` [package README](https://cran.r-project.org/web/packages/scales/readme/README.html) – Gregor Thomas Nov 26 '19 at 14:52
  • Hi @TreePete, the easier solution is really to use x as the x axis. Remember, even if you regress, the confidence interval created for your response variable (y axis). As long the pre-transformation x values match the prediction, it's all good. – StupidWolf Nov 26 '19 at 15:38
  • Thanks for the answer, but if I use x instead of x.s regression and CI won´t fit, I edited the Question and inclueded figures from my dataset – TreePete Nov 26 '19 at 16:53

1 Answers1

1

There's a 1-to-1 linear mapping relationship between x and x.s, so one way you can go about this is to specify the desired labels in x's scale, and the corresponding breaks in x.s's scale:

ggplot(df, aes(x = x.s, y = y))+
  geom_point()+
  scale_x_continuous(labels = seq(0, 100, 10),
                     breaks = predict(lm(x.s ~ x, data = df),
                                      newdata = data.frame(x = seq(0, 100, 10)))) +
  labs(x = "Canopy openness [%]", y = "Rarefied richness") + 
  theme_bw()
Z.Lin
  • 28,055
  • 6
  • 54
  • 94