简体   繁体   English

以R为单位的频率图

[英]Plot of frequencies with counts in R

The frequency plot that I'm trying to do is 我想做的频率图是

  • Barplot with counts above each bar 条形图,每个条形以上
  • Relative frequency in left side 左侧相对频率
  • Cumulative frequency in right side 右侧累计频率

The dataset is 数据集是

dput(x2)
c(1L, 5L, 3L, 3L, 5L, 3L, 4L, 1L, 2L, 2L, 7L, 3L, 2L, 2L, 3L, 
3L, 2L, 1L, 5L, 4L, 4L, 3L, 5L, 2L, 6L, 2L, 1L, 2L, 5L, 5L, 5L, 
3L, 6L, 4L, 5L, 4L, 6L, 7L)

The distribution of frequencies are 频率分布是

table(x2)
x2
1 2 3 4 5 6 7 
4 8 8 5 8 3 2 

The relative frequencies are 相对频率是

prop.table(table(x2))
x2
         1          2          3          4          5          6          7 
0.10526316 0.21052632 0.21052632 0.13157895 0.21052632 0.07894737 0.05263158 

EDIT: Like in the image below, but with cumulative frequency in the right side, relative frequency in the left and the bars with counts 编辑:如下图所示,但累积频率在右侧,相对频率在左侧,带计数的条形图 在此处输入图片说明


library(tidyverse)
library(broom)

table(x2) %>% 
  tidy() %>%
  mutate(rel_freq = Freq/sum(Freq), sum = sum(Freq)) %>%
  ggplot(aes(reorder(x2, Freq), rel_freq)) + 
  geom_col() + 
  geom_text(aes(label = Freq), vjust = -.5) +
  scale_y_continuous(sec.axis = sec_axis(~.*length(x2))) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM