guyabel
guyabel

Reputation: 8366

Strange geom_vline() behavior in ggplot when using coord_trans() with custom transformation

I am trying to use a log-modulus transformation in my plot. It was working fine...

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

When I tried to add a geom_vline() things got weird...

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

Any idea how to get geom_vline() to go from the top to the bottom of the plot window... or a work around hack?

Upvotes: 2

Views: 124

Answers (1)

missuse
missuse

Reputation: 19746

Here is a solution using 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

Upvotes: 2

Related Questions