使用coord_trans()和自定义转换时,ggplot中的奇怪geom_vline()行为

时间:2017-11-17 16:05:29

标签: r ggplot2 scaletransform

我想在我的情节中使用log-modulus transformation。它工作得很好......

library(tidyverse)
library(scales)

log_modulus_trans <- function() 
  trans_new(name = "log_modulus", 
            transform = function(x) sign(x) * log(abs(x) + 1), 
            inverse = function(x) sign(x) * ( exp(abs(x)) - 1 ))

# fake data
set.seed(1)
d <- data_frame(
  tt = rep(1:10, 3),
  cc = rep(LETTERS[1:3], each = 10),
  xx = c(rnorm(10, mean = 100, sd = 10), 
         rnorm(10, mean = 0, sd = 10),
         rnorm(10, mean = -100, sd = 10)))

ggplot(data = d, 
       mapping = aes(x = tt, y = xx, group = cc)) +
  geom_line() + 
  coord_trans(y = "log_modulus")

enter image description here

当我尝试添加geom_vline()时,事情变得奇怪......

ggplot(data = d, 
       mapping = aes(x = tt, y = xx, group = cc)) +
  geom_line() + 
  coord_trans(y = "log_modulus") +
  geom_vline(xintercept = 5) 

enter image description here

知道如何让geom_vline()从情节窗口的顶部到底部......或者解决黑客的问题吗?

1 个答案:

答案 0 :(得分:2)

以下是使用geom_segment

的解决方案
ggplot(data = d, 
       mapping = aes(x = tt, y = xx, group = cc)) +
  geom_line() + 
  geom_segment(x = 5, xend = 5, y = -150, yend = 150) +
  coord_trans(y = "log_modulus")

enter image description here