如何在R中使用Plotly制作水平累积直方图(从最大到最小)

时间:2019-03-08 21:29:20

标签: r ggplot2 plotly r-plotly

我正在使用R中的plotly包制作累积直方图,下面是图表。

enter image description here

我想知道如何将其转换为水平累积直方图,并从最大到最小(从上到下)进行绘制。例如,以11比1代替1比11。

我问的原因是因为最大的数字只有很少的记录,如果我们水平绘制直方图,从顶部开始显示更大的数字会更加清楚。

我还尝试绘制条形图以获取与通过指定orientation = 'h'可以水平绘制条形图相同的数据,但是,条形图不能累积。

谢谢。

1 个答案:

答案 0 :(得分:2)

ggplot中,您可以使用coord_flip()

require(tidyverse)

mtcars %>% 
  ggplot(aes(disp)) +
  geom_histogram(aes(y = cumsum(..count..)),
                 binwidth = 1, boundary = 0) + 
  coord_flip() 

enter image description here

要颠倒顺序,您可以使用scale_x_reverse()

mtcars %>% 
  ggplot(aes(disp)) +
  geom_histogram(aes(y = cumsum(..count..)),
                 binwidth = 1, boundary = 0) + 
  coord_flip() +
  scale_x_reverse()

enter image description here