0

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.

Socsi2
  • 33
  • 3
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What was the problem you encountered when trying to produce a smaller data.frame that was similar to your data? Why is it so important to avoid the "key" like in in the other question? – MrFlick Aug 31 '23 at 15:07
  • using 'key' seems to produce a legend for the bar chart, which is redundant for my purposes and takes up quite a lot space. I can't use a sample of my data for ethical reasons and am not sure how to create sample data that is similar enough to it. – Socsi2 Aug 31 '23 at 15:14

2 Answers2

0

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()

That results in enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • That seems to work well! Thank you so much for your help. – Socsi2 Aug 31 '23 at 15:29
  • how do I reorder the bars in ascending order for this example? I'm not sure how to call out the x and y variables in this instance. – Socsi2 Aug 31 '23 at 16:23
0

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="")
  • Thanks @QuinnWhite. how would I go about re-ordering the bars in ascending order in this example? – Socsi2 Aug 31 '23 at 16:33
  • The `forcats` package is useful for this. You can do: `long_dat %>% arrange(name) %>% filter(value == TRUE) %>% ggplot(aes(x=n, y =fct_reorder(name,n, .desc=TRUE))) + geom_bar(stat="identity") + labs(x = "Number TRUE", y="")` – Quinn White Sep 01 '23 at 17:26