假设我有一个简单的数据框,其中包含类别,计数和份额,我想使用 ggplot
cat1 <- c("category1",
"category2",
"category3",
"category4",
"category5")
count <- c(12, 43, 31, 25, 11)
df <- data.frame(cat1, count)
df$share <- df$count / sum(df$count) * 100
require(ggplot2)
ggplot(df, aes(cat1, count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = round(count, 2)), vjust = "bottom", size = 5)
有没有办法像下面的屏幕截图所示那样,将计数的份额显示在计数旁边的方括号中,作为每个条顶部的标签(取自此博客)?
尝试这个
cat1 <- c("category1",
"category2",
"category3",
"category4",
"category5")
count <- c(12, 43, 31, 25, 11)
df <- data.frame(cat1, count)
df$share <- df$count / sum(df$count) * 100
df$label <- paste0(round(df$count, 2), " (", round(df$share, 1), "%)")
require(ggplot2)
#> Lade nötiges Paket: ggplot2
ggplot(df, aes(cat1, count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = label), vjust = "bottom", size = 5)
由reprex软件包(v0.3.0)创建于2020-03-25
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句