可以在功能上执行类似于R中的hjust / vjust或position_dodge的功能吗?

时间:2019-04-22 20:36:01

标签: position plotly data-visualization r-plotly ggplotly

我希望调整哑铃图中的点和线的位置,以分隔横条而不是覆盖横条,类似于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

我希望它看起来像这样: static viz

但是国家名称在y轴上。

有没有办法调整相对于离散轴点的垂直高度?

1 个答案:

答案 0 :(得分:0)

更新:这不是很优雅,但我想出了一种解决方法,方法是用y轴部分覆盖y轴!仍然会喜欢一个更好的答案,但这是一个可用的解决方案!

' '.join(row.text for row in positive.select('text').collect())