ggplot2 - Plot multiple truefalse columns as bar plot in R - Stack Overflow

I have a dataframe that looks like this:structure(list(chesapeakebay = c(TRUE, TRUE, TRUE, TRUE, TRUE,

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744233642a4564372.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信