0
votes

I'm looking to create a proportional bar graph in R. I have two variables, one is "drunk" and one is "housing“。I want to graph the proportional of individuals of each category of " housing" who fulfil "drunk".

The dataframe is something like follows:

Drunk: 0, 1, 0, 1, 1, 1, 1, 0 Housing: 1, 2, 1, 3, 1, 4, 1, 1

I want to know how to graph the proportion of each category of housing (1 through 4) which fulfils "1" for drunk.

png("Graphs/Analysis_Figure1.png")

analysis %>%

count(housing, drunk) %>%

group_by(housing) %>%

mutate(freq = n/sum(n)) %>%

ggplot(aes(x = housing, y = freq, fill = drunk)) +

geom_bar(stat="identity", position = 'dodge')

dev.off()

This gives me the proportions, but instead of only including "1" responses for drunk, I also get "0". Is there a way to only include 1?

This is an image I generated with STATA, which I hope to replicate inN R

This is what I get with R instead. I want to remove the "2 or less" (coded as "0" drunk responses

1

1 Answers

0
votes

You can use the filter() function from dplyr in your pipleline:

analysis %>%
filter(drunk == 1) %>%
count(housing, drunk) %>% ...