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
  • 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
Add a comment  | 

2 Answers 2

Reset to default 5

Pivot 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