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?