steve
steve

Reputation: 603

exponentiating x-axis (but not the logged linear relationship) in ggplot2

I'm working on charted predicted probabilities from a mixed effects logit model with a cross-level interaction in ggplot2. I calculated the predicted probabilities and was able to plot the small interaction observed from the models.

The working example

What I would like to do, if it all possible with ggplot2 (I'm sure it is) is transform the x-axis there to the original (i.e. exponentiated) scale. The x variable in my working example (rangeofx) is the logged and centered values of x from my model. But, I don't want to simply exponentiate the relationship because it would be non-linear and that wasn't what was estimated in the model. I included the un-transformed x values in the working example (untransformedx).

How do I do this with the ggplot2 package? All else equal, I'd be fine with presenting something like this (perhaps adding a legend and making those lines more readable in black and white) and making the reader follow along, but reviewers in my discipline tend to hate that cavalier attitude and would want to see the unintuitive centered-logged values of x correspond with the more intuitive values that are bounded between 0 and 100. This would be appropriate if I wanted to communicate predicted probabilities at untransformed values like 5, 10, 25, 50, 75, and so on. That'll be the next thing I want to do.

Any help here would be appreciated. Working example follows. Thanks.

library(ggplot2)
library(scales)

Example <- read.csv("http://dl.dropbox.com/s/jyxdo6jgzf72yn5/ggplot2-example.csv")
summary(Example)

ExampleGraph <- ggplot(Example, aes(rangeofx, p.group1)) + geom_line() +
    geom_line(aes(rangeofx, p.group2), color="red") +
    geom_line(aes(rangeofx, p.group3), color="blue") +
    theme_bw() + opts(title="The Effect of X on Y \n Among the Three Groups") + xlab("Range of X (Logged)") + ylab("Pr(Y)")

ggsave(file = "output.png")

Upvotes: 1

Views: 1539

Answers (2)

Jaap
Jaap

Reputation: 83265

You could use scale_x_sqrt on untransformedx:

# transforming your data from wide to long
require(reshape2)
ex2 <- melt(Example, id=c("rangeofx","untransformedx"))

# creating the plot
ggplot(ex2, aes(x=untransformedx, y=value, color=variable)) + 
  geom_line() +
  scale_x_sqrt(breaks=c(1,5,10,25,50,75)) +
  theme_bw() + 
  labs(title="The Interaction between X and Y \n Among the Three Groups", x="Range of X (Logged)", y="Pr(Y)")

the result: enter image description here

Or without adjusting the x-axis:

ggplot(ex2, aes(x=untransformedx, y=value, color=variable)) + 
  geom_line() +
  theme_bw() + 
  labs(title="The Interaction between X and Y \n Among the Three Groups", x="Range of X (Logged)", y="Pr(Y)")

which gives: enter image description here

Upvotes: 3

Paulo E. Cardoso
Paulo E. Cardoso

Reputation: 5856

I didn't understand what you want with exponentiation of your x var but for the rest try

library(reshape2)

dat <- melt(Example, id = c('rangeofx', 'untransformedx'),
            variable.name = "group")
ggplot(dat, aes(rangeofx, value)) + 
  geom_line(aes(colour = group)) +
  theme_bw() + 
  labs(title="The Interaction between X and Y \n Among the Three Groups", 
       x = "Range of X (Logged)", y ="Pr(Y)") +
  scale_colour_grey()

ggplot example with labels

or with untransformed x variable

ggplot(dat, aes(untransformedx, value)) + 
  geom_line(aes(colour = group)) +
  scale_x_sqrt() +
  theme_bw() + 
  labs(title="The Interaction between X and Y \n Among the Three Groups", 
       x = "Range of X (Logged)", y ="Pr(Y)") +
  scale_colour_grey()

untransformed x

Upvotes: 1

Related Questions