Reputation: 65
I have a data frame with 88 observations (rows). There are 13 columns, in which every entry either has the value TRUE or FALSE.
Using ggplot(preferably), how do I get a barplot with flipped coordinates that has the name of the 13 columns on the x-axis and the number of TRUEs of every column as the height of the bars?
There was a similar question answered here [enter link description here][1] but it seems to require the use 'key' in order to work, which I'd rather avoid.
Sadly, I don't have sample data. I couldn't figure out how to produce a smaller data frame that fits the description of my data.
Upvotes: 0
Views: 48
Reputation: 1
Here is a sample data frame based on the description you provided. We can sample 1's and 0's from the Bernoulli and then convert those vectors to logical vectors.
Once we have the data frame, we can restructure it to get the plot you're looking for.
library(tidyverse)
dat <- map(1:13, ~rbinom(n=88, size=1, prob=.5))
# naming columns alphabetically for example data frame
names(dat) <- letters[1:13]
dat <- dat %>%
as_tibble() %>%
mutate(across(everything(), as.logical))
long_dat <- dat %>%
# restructure data frame
pivot_longer(everything()) %>%
group_by(name, value) %>%
# number of TRUE's and FALSE's for each
summarize(n=n())
long_dat %>%
arrange(name) %>%
filter(value == TRUE) %>%
ggplot(aes(x=n, y =name)) +
geom_bar(stat="identity") +
labs(x = "Number TRUE",
y="")
Upvotes: 0
Reputation: 206411
I'm guessing your data looks something like this
set.seed(1234)
dd <- as.data.frame(Map(function(...) sample(c(T, F), 25, replace = TRUE), letters[1:5]))
If that's the cause, you need to reshape your data so ggplot can plot it. You can do
library(ggplot2)
dd %>%
tidyr::pivot_longer(everything(), names_to="column", values_to="value") %>%
dplyr::filter(value == TRUE) %>%
ggplot() +
aes(column) +
geom_bar() +
coord_flip()
Upvotes: 0