是否有功能在R中执行类似于hjust / vjust或position_dodge的操作?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否有功能在R中执行类似于hjust / vjust或position_dodge的操作?相关的知识,希望对你有一定的参考价值。

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

以上是关于是否有功能在R中执行类似于hjust / vjust或position_dodge的操作?的主要内容,如果未能解决你的问题,请参考以下文章

R语言ggplot2可视化使用vjust和hjust参数对齐图像中的文本注释信息(左对齐右对齐居中)实战

Java对象类似于R data.frame [关闭]

【R】热图绘制

在 R 中测量函数执行时间

在 R 中测量函数执行时间

在 R 中测量函数执行时间