使用日志转换后,将ggplot比例标签更改为原始值

时间:2018-04-30 13:47:03

标签: r ggplot2

我有exponentially分布式变量,我想使用ggplot2进行绘制。我将采用变量的log。但是,我不希望轴标签为log格式,而是希望它是原始的exponentially分布式值。这是一个例子。

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')

我希望x上有log_x个变量标签。

aero_plot

enter image description here

2 个答案:

答案 0 :(得分:1)

我开始使用它,但问题是你可以在x图中看到正常的log_x标签。

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') 

答案 1 :(得分:1)

ggplot&#39; s scale_x_log10可能需要救援吗?我并非100%确定我理解您的问题,因为我不了解您的示例代码。希望这就是你的意思......

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