我希望在哑铃图中调整点和线的位置以分隔条而不是覆盖它们,类似于R中的位置闪避或调整/调整。
下面的代码产生了一些接近我想要的东西,但是杠铃被覆盖了。
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
通过将国家名称强制转换为一个因子,我可以获得理想的间距,但我失去了我希望保留的国家标签。我尝试一起使用国家和数字因子索引,但是在情节上不允许离散和连续的比例。
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
我希望它看起来像这样:
但是y轴上的国家名称。
有没有办法调整相对于离散轴点的垂直高度?
更新:它不优雅,但我通过用y轴覆盖y轴找出了解决方法!仍然会喜欢更好的答案,但这是一个可用的解决方案!
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 = ""))
)