admin管理员组文章数量:1400157
I have a dataframe that looks like this:
structure(list(chesapeakebay = c(TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE), icefishing = c(FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE), lakesimpoundments = c(FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE),
nontidal = c(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE,
FALSE, FALSE, TRUE), tidal = c(FALSE, TRUE, FALSE, TRUE,
TRUE, TRUE, TRUE, FALSE, TRUE, TRUE)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
I want to create a bar chart similar to this with the percentage of rows answering true for each column.
Is this possible?
I have a dataframe that looks like this:
structure(list(chesapeakebay = c(TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE), icefishing = c(FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE), lakesimpoundments = c(FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE),
nontidal = c(FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE,
FALSE, FALSE, TRUE), tidal = c(FALSE, TRUE, FALSE, TRUE,
TRUE, TRUE, TRUE, FALSE, TRUE, TRUE)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
I want to create a bar chart similar to this with the percentage of rows answering true for each column.
Is this possible?
Share Improve this question asked Mar 24 at 18:37 Ryan GaryRyan Gary 17710 bronze badges 1 |2 Answers
Reset to default 5Pivot to long, then compute n and percentage using sum()
and mean()
:
library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
dat |>
pivot_longer(everything()) |>
summarize(
n = sum(value),
lab = paste0(n, " (", percent(mean(value)), ")"),
.by = name
) |>
ggplot(aes(n, name)) +
geom_col(fill = "darkmagenta") +
geom_text(aes(label = lab), nudge_x = 1) +
scale_x_continuous(limits = c(0, 11.5), breaks = seq(0, 10, by = 2)) +
labs(x = NULL, y = NULL) +
theme_minimal()
Here's a base R approach:
bar_plot <- function(data) {
opar <- par(mar=c(3,10,1,5))
y <- colSums(data)
bp <- barplot(y, horiz=TRUE, las=1, col="purple4")
text(y, bp,
labels=paste0(y, " (", round(100*y/nrow(data)), "%)"),
pos=4, cex = 0.8, xpd=TRUE)
par(opar)
}
bar_plot(df)
本文标签: ggplot2Plot multiple truefalse columns as bar plot in RStack Overflow
版权声明:本文标题:ggplot2 - Plot multiple truefalse columns as bar plot in R - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744233642a2596456.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
library(tidyverse); df1 |> pivot_longer(everything()) |> summarize(total = sum(value), share = total / n(), .by = name) |> ggplot(aes(total, name)) + geom_col() + geom_text(aes(label = glue::glue("{total} ({scales::percent(share, accuracy = 0.1)})" )), hjust = 0)
– Jon Spring Commented Mar 24 at 18:52