绘制r:can can can can添加段
基本上,我有这个 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)
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'))
Any tips here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是添加细分市场会破坏您的酒吧的顺序。但是,适应此 post 在示例中,您可以通过手动通过x轴通过
布局:
修复您的第二个问题:适应此 post 您可以通过转换
键来切换到连续的规模
变量到数字并通过布局
: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
: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 vialayout
: