[英]Plot of frequencies with counts in R
我想做的頻率圖是
數據集是
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)
頻率分布是
table(x2)
x2
1 2 3 4 5 6 7
4 8 8 5 8 3 2
相對頻率是
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
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.