jonocarroll/ggeasy

coord flip legends in coord flip plots

softloud opened this issue · 3 comments

On the banner on my twitter account (cantabile) I've got a vis where I've coord flipped but the legend didn't. Is there an easy way to do this? Can we manipulate elements of a legend?

John MacKintosh suggested this resource. Looks like a good place to get started.

Reference:

cantabile_banner

Is this a geom_pointrange()? I can't see an obvious way to do that, but it's not an unheard of request: tidyverse/ggplot2#1389. The suggestion there is to use ggstance::pointrangeh() and avoid the coord_flip().

library(ggplot2)
library(dplyr)

## example data
df <- data.frame(x = 1:100,
                 group = c(rep(TRUE, 50), rep(FALSE, 50)),
                 v = seq(-4, 4, length.out = 100)) %>%
  mutate(y = tanh(v),
         lower = tanh(v) - 0.2,
         upper = tanh(v) + 0.2)

## vertical pointranges
ggplot(df, aes(x, y, colour = group)) + 
  geom_pointrange(aes(ymin = lower, ymax = upper))

## horizontal pointranges via coord_flip, legend still vertical
ggplot(df, aes(x, y, colour = group)) + 
  geom_pointrange(aes(ymin = lower, ymax = upper)) + 
  coord_flip()

## horizontal pointrange with legend the right way
ggplot(df, aes(y, x, colour = group)) + 
  ggstance::geom_pointrangeh(aes(xmin = lower, xmax = upper))

If an accessible way to alter the legend line angle comes up then it will fit in fine here. Until then, I think the ggstance workaround is the only option. I'm happy for this to be reopened if anyone has an idea.

Has this ever been resolved? I have the same problem
PRSCT_rgs2021-07-07

@topherhuebel : please see this toy example. Instead of using coord_flip(), I would change the x and y.

Reference: https://ggplot2.tidyverse.org/reference/geom_linerange.html

This is the data

data <- tribble(
  ~Treat, ~Group, ~prop, ~prop_lb, ~prob_up, 
  "T1", "G1", 0.7, 0.5, 0.9, 
  "T1", "G2", 0.3, 0.2, 0.4, 
  "T2", "G1", 0.4, 0.35, 0.45, 
  "T2", "G2", 0.6, 0.55, 0.65
)

If we don't use coord_flip(), we have vertical line and vertical legend figures

ggplot(data, 
       aes(Treat, prop, color=Group))+
  geom_linerange(aes( ymin=prop_lb, ymax=prob_up), width=0.2, size=0.5, position = "identity",)+
  labs(title="My title",
       x ='Treatment ', y = 'Event proportion')+
   ylim(-0.01, 1.01)+
   theme(legend.direction='vertical')

ver_line_ver_legend

when we use coord_flip(), we have horizontal lines and vertical legend figures

ggplot(data, 
       aes(Treat, prop, color=Group))+
  geom_linerange(aes( ymin=prop_lb, ymax=prob_up), width=0.2, size=0.5, position = "identity",)+
  labs(title="My title",
       x ='Treatment ', y = 'Event proportion')+
  ylim(-0.01, 1.01)+
  theme(legend.direction='vertical')+
  coord_flip()

hor_line_ver_legend

So, instead of using coord_flip, we would change the role of x and y, we will have horizontal lines and horizontal legend figures

ggplot(data, 
       aes(prop, Treat, color=Group))+
  geom_linerange(aes( xmin=prop_lb, xmax=prob_up), width=0.2, size=0.5, position = "identity",)+
 
  labs(title="My title",
       x ='Treatment ', y = 'Event proportion')+
   xlim(-0.01, 1.01)+
   theme(legend.direction='vertical')

hor_line_hor_legend