elliot
elliot

Reputation: 1944

Change ggplot scale labels to original values after using a log transformation

I've got an exponentially distributed variable that I'd like to plot using ggplot2. I'm going to take the log of the variable. However, instead of having the axis label be the log format, I'd like it to be the original exponentially distributed values. Here's an example.

set.seed(1000)

aero_df <- 
data_frame(
    x = rnorm(100,100,99),
    y = sample(c('dream on',
                 'dude looks like a lady'),
               100,
               replace = T)) %>% 
    mutate(x = x*x,
           log_x = log(x)) %>% 
    gather(key,value,-y)

aero_plot <-   ggplot(aero_df,aes(value,color = y,fill = y))+
    geom_density(show.legend = F)+
    facet_wrap(key~y,scales = 'free')

I'd like to have the x variable labels on the log_x.

aero_plot

enter image description here

Upvotes: 2

Views: 2757

Answers (2)

LetEpsilonBeLessThanZero
LetEpsilonBeLessThanZero

Reputation: 2403

ggplot's scale_x_log10 to the rescue, maybe? I'm not 100% sure I understand your question, because I didn't understand your example code. Hopefully this is what you mean...

library(tidyverse)

set.seed(1000)

aero_df <- 
  data_frame(
    x = rnorm(100,100,99),
    y = sample(c('dream on',
                 'dude looks like a lady'),
               100,
               replace = T))

aero_plot <- ggplot(aero_df,aes(x,color = y,fill = y)) +
  geom_density(show.legend = F) +
  scale_x_log10() +
  facet_wrap(~y,scales = 'free')

print(aero_plot)

enter image description here

Upvotes: 1

Wietze314
Wietze314

Reputation: 6020

I started of with this, but the issue here is that you can see the normal log_x labels also in the x plots.

ticks <- c(3,6,9,12)
logticks <- c(exp(9),exp(10),exp(11))

ggplot(aero_df,aes(value,color = y,fill = y))+
  geom_density(show.legend = F)+
  scale_x_continuous(breaks = c(ticks,logticks), labels = c(ticks,log(logticks))) +
  facet_wrap(key~y,scales = 'free') 

Upvotes: 2

Related Questions