绘制r:can can can can添加段

发布于 2025-01-23 06:01:06 字数 1138 浏览 3 评论 0原文

基本上,我有这个 dataframe

# dataframe
df2 = data.frame(value = c(9, 2, 7, 5, 6),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))

这是i 需要的图。我正在使用重新订购,因为我知道这是正确订购栏的好方法。

library(tidyverse)
df2 %>% ggplot() + 
  geom_col(aes(reorder(key, -value), value), fill = 'grey') + 
  geom_hline(yintercept = 4)

正确的绘图


我试图在plotly中进行此操作,但是每次我使用重新排序时,它都无法正常工作。

另外,我希望在第一个栏的极端左侧开始,并在最后一个bar的极端右侧结束。

# using reorder to order the bars doesn't work!
df2 %>% 
  plot_ly(x = ~reorder(key, -value), # <- here
          y = ~value,
          color = I('grey'),
          type = 'bar') %>% 
  add_segments(x = 'ar', xend = 'or',
               y = 4, yend = 4, 
               color = I('black'))

错误的情节

这里有任何提示吗?

Basically, I have this dataframe:

# dataframe
df2 = data.frame(value = c(9, 2, 7, 5, 6),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))

And this is the plot I need. I'm using reorder, because I know it is a good way to order the bars correctly.

library(tidyverse)
df2 %>% ggplot() + 
  geom_col(aes(reorder(key, -value), value), fill = 'grey') + 
  geom_hline(yintercept = 4)

CORRECT PLOT


I'm trying to do this in plotly, but everytime I use reorder, it doesn't work properly.

Also, I would like the segment to start in the extreme left side of the first bar and end in the extreme right side of the last bar.

# using reorder to order the bars doesn't work!
df2 %>% 
  plot_ly(x = ~reorder(key, -value), # <- here
          y = ~value,
          color = I('grey'),
          type = 'bar') %>% 
  add_segments(x = 'ar', xend = 'or',
               y = 4, yend = 4, 
               color = I('black'))

WRONG PLOT

Any tips here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2025-01-30 06:01:06

问题是添加细分市场会破坏您的酒吧的顺序。但是,适应此 post 在示例中,您可以通过手动通过x轴通过布局:

library(plotly)

xform <- list(categoryorder = "array",
              categoryarray = levels(reorder(df2$key, -df2$value)))

df2 %>% 
  plot_ly(x = ~key,
          y = ~value,
          color = I('grey'),
          type = 'bar') %>% 
  add_segments(x = 'ar', xend = 'or',
               y = 4, yend = 4, 
               color = I('black')) %>%
  layout(xaxis = xform)

“”

修复您的第二个问题:适应此 post 您可以通过转换键来切换到连续的规模变量到数字并通过布局

df2 %>%
  plot_ly(
    x = ~ as.numeric(reorder(key, -value)),
    y = ~value,
    color = I("grey"),
    type = "bar"
  ) %>%
  add_segments(
    x = .6, xend = 5.4,
    y = 4, yend = 4,
    color = I("black")
  ) %>%
  layout(
    xaxis = list(
      ticktext = levels(reorder(df2$key, -df2$value)),
      tickvals = seq_along(df2$key),
      tickmode = "array",
      range = c(0.5, 5.5)
    )
  )

“

The issue is that adding the segments breaks the order of your bars. However, adapting this post to your example you could achieve your desired result by manually setting the order of the x axis via layout:

library(plotly)

xform <- list(categoryorder = "array",
              categoryarray = levels(reorder(df2$key, -df2$value)))

df2 %>% 
  plot_ly(x = ~key,
          y = ~value,
          color = I('grey'),
          type = 'bar') %>% 
  add_segments(x = 'ar', xend = 'or',
               y = 4, yend = 4, 
               color = I('black')) %>%
  layout(xaxis = xform)

To fix your second issue: Adapting this post you could switch to a continuous scale by converting your key variable to a numeric and setting the axis labels via layout:

df2 %>%
  plot_ly(
    x = ~ as.numeric(reorder(key, -value)),
    y = ~value,
    color = I("grey"),
    type = "bar"
  ) %>%
  add_segments(
    x = .6, xend = 5.4,
    y = 4, yend = 4,
    color = I("black")
  ) %>%
  layout(
    xaxis = list(
      ticktext = levels(reorder(df2$key, -df2$value)),
      tickvals = seq_along(df2$key),
      tickmode = "array",
      range = c(0.5, 5.5)
    )
  )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文