df <- data.frame(
cola = c('1',NA,'c','1','1','e','1',NA,'c','d'),
colb = c("a",NA,"c","d",'a','b','c','d','c','d'),
colc = c('a',NA,'1','d','a',NA,'c',NA,'c','d'),stringsAsFactors = TRUE)
table(df$cola)
上述R脚本的输出为:
1 c d e
4 2 1 1
答案 0 :(得分:4)
我们将列gather
设为'long'格式,然后执行ggplot
library(tidyverse)
df %>%
# gather to long format
gather(na.rm = TRUE) %>%
# get the frequency count of key, value columns
count(key, value) %>%
ggplot(., aes(x = value, y = n)) +
geom_bar(stat = "identity") +
# facet wrap with key column
facet_wrap(~ key)
答案 1 :(得分:3)
尝试一下
library(tidyverse)
df %>%
map(function(x){as.data.frame(table(x))}) %>%
bind_rows(.id = "variable") %>%
ggplot(aes(x = x, y = Freq)) +
geom_col() +
facet_wrap(~variable)