0

I'm hoping to adjust the location of points and lines in a dumbbell plot to separate the bars rather than overlaying them, similar to position dodge or hjust/vjust in R.

The code below produces something close to what I'd like, but the barbells are overlayed.

urlfile <- 'https://raw.githubusercontent.com/charlottemcclintock/GenSquared/master/data.csv'
df <- read.csv(urlfile)

p <- plot_ly(df, color = I("gray80")) %>%
  add_segments(x = ~mom, xend = ~daughter, y = ~country, yend = ~country, showlegend = FALSE) %>%
  add_markers(x = ~mom, y = ~country, name = "Mother", color = I("purple")) %>%
  add_markers(x = ~daughter, y = ~country, name = "Daughter", color = I("pink")) %>%
  add_segments(x = ~dad, xend = ~son, y = ~country, yend = ~country, showlegend = FALSE) %>%
  add_markers(x = ~dad, y = ~country, name = "Father", color = I("navy")) %>%
  add_markers(x = ~son, y = ~country, name = "Son", color = I("blue")) %>%
  layout(
    title = "Gender educational disparity",
    xaxis = list(title = "Mean Years of Education"),
    margin = list(l = 65)
  )
p

By coercing the country names to a factor, I can get the ideal spacing but I lose the country labels which I'm hoping to keep. I tried using country and numeric factor index together but plotly doesn't allow discrete and continuous scales together.

df$cnum <- as.numeric(as.factor(df$country))
p <- plot_ly(df, color = I("gray80")) %>%
  add_segments(x = ~mom, xend = ~daughter, y = ~cnum+.2, yend = ~cnum+0.2, showlegend = FALSE) %>%
  add_markers(x = ~mom, y = ~cnum+.2, name = "Mother", color = I("purple")) %>%
  add_markers(x = ~daughter, y = ~cnum+.2, name = "Daughter", color = I("pink")) %>%
  add_segments(x = ~dad, xend = ~son, y = ~cnum-.2, yend = ~cnum-.2, showlegend = FALSE) %>%
  add_markers(x = ~dad, y = ~cnum-.2, name = "Father", color = I("navy")) %>%
  add_markers(x = ~son, y = ~cnum-.2, name = "Son", color = I("blue")) %>%
  layout(
    title = "Gender educational disparity",
    xaxis = list(title = "Mean Years of Education"),
    margin = list(l = 65)
  )
p

I would like it to look like this: static viz

But with the country names on the y-axis.

Is there a way to adjust the vertical height relative to a discrete axis point?

charlottemcc
  • 47
  • 1
  • 7

1 Answers1

0

Update: it's not elegant but I figured out a workaround by overwriting the y axis with a section y axis! Would still love a better answer, but this is a usable fix!

df$arb=15
plot_ly(df, color = I("gray80")) %>%
            add_segments(x = ~mom, xend = ~daughter, y = ~cnum+.2, yend = ~cnum+.2, showlegend = FALSE) %>%
            add_markers(x = ~mom, y = ~cnum+.2, name = "Mother", color = I("purple"), size=2) %>%
            add_markers(x = ~daughter, y = ~cnum+.2, name = "Daughter", color = I("pink"), size=2) %>%
            add_segments(x = ~dad, xend = ~son, y = ~cnum-.1, yend = ~cnum-.1, showlegend = FALSE) %>%
            add_markers(x = ~dad, y = ~cnum-.1, name = "Father", color = I("navy"), size=2) %>%
            add_markers(x = ~son, y = ~cnum-.1, name = "Son", color = I("blue"), size=2) %>%
            add_markers(x = ~arb, y = ~country, name = " ", color = I("white"), yaxis = "y2") %>%
            layout(
                yaxis=list(title="", tickfont=list(color="white")),
                yaxis2 = list(overlaying = "y", side = "left", title = ""))
            ) 
charlottemcc
  • 47
  • 1
  • 7