geom_dumbbell with coord_trans

525 Views Asked by At

I am trying to generate a dumbbell plot in ggplot, while also trying to log transform the x and y axes. System throws an error that I can't understand. Looking for some help.

Here is the sample code:

#create the data
df <- data.frame(x = c(1, 2, 3, 100),
                 xend = c(2, 4, 6, 110),
                 y = c(1, 2, 3, 100)) 

#plot the untransformed dumbbell plot
library('ggalt')
ggplot(df, aes(x = x, xend = xend, y = y)) + 
  geom_dumbbell()

enter image description here

#Try the plot with coordinate transformation
ggplot(df, aes(x = x, xend = xend, y = y)) + 
  geom_dumbbell() + 
  coord_trans(x = 'log2', y = 'log2')

Throws error:

Error in [.data.frame(df, , c("alpha", "colour", "size", "linetype")) :
undefined columns selected

If I amend coord_trans so that coord_trans(x = 'log2', xend = 'log2', y = 'log2), I get the error:

Error in coord_trans(x = "log2", xend = "log2", y = "log2") :
unused argument (xend = "log2")

What I want is the equivalent of this, with geom_dumbbell() rather than geom_point():

ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  coord_trans(x = 'log2', y = 'log2')

enter image description here

Any thoughts on how I can get geom_dumbbell() to work with coord_trans()?

1

There are 1 best solutions below

1
On BEST ANSWER

That is weird, I don't know why it doesn't work with geom_dumbbell. I also didn't know that geom_dumbbell exists! So here is a hack I've been doing for years using geom_linerange and geom_point to build the components:

ggplot(df, aes(x=x, y = y)) + 
  geom_linerange(aes(xmin=x, xmax=xend)) + 
  geom_point() + 
  geom_point(aes(x=xend)) +
  coord_trans(x = 'log2', y = 'log2')