0

I'm new to R and trying to get this data in a 100% Stacked Bar chart in R to look like this

img1

The data looks like this

img2

ccEFFECT <- data$Q7_1
ccEFFECT [ccEFFECT == -99] <- NA

ccEFFECTS<- factor(ccEFFECT , labels = c("Strongly Disagree", "Disagree", "Neither Agree nor Disagree", "Agree", "Strongly Agree"))
levels(ccEFFECTS )
str(ccEFFECTS )
summary (ccEFFECTS )

length(na.omit(ccEFFECTS ))
length(ccEFFECTS )
ccEFFECTfrequency <- table (ccEFFECTS ) #frequency
ccEFFECTfrequency
#percentages
ccEFFECT_PERCENTAGE=prop.table(table(ccEFFECTS)) * 100
ccEFFECT_PERCENTAGE

barplot(ccEFFECT_PERCENTAGE)



Q2EFFECT<- data$Q7_2
Q2EFFECT [Q2EFFECT == -99] <- NA



Q2EFFECTS<- factor(Q2EFFECT , labels = c("Strongly Disagree", "Disagree", "Neither Agree nor Disagree", "Agree", "Strongly Agree"))
levels(Q2EFFECTS )# how many levels of a categorical variable
str(Q2EFFECTS )
summary (Q2EFFECTS )

length(na.omit(Q2EFFECTS ))
length(Q2EFFECTS )
Q2EFFECTfrequency <- table (Q2EFFECTS ) #frequency
Q2EFFECTfrequency
#percentages
Q2EFFECT_PERCENTAGE=prop.table(table(Q2EFFECTS)) * 100
Q2EFFECT_PERCENTAGE

barplot(Q2EFFECT_PERCENTAGE)

Any suggestions.

user438383
  • 5,716
  • 8
  • 28
  • 43
James
  • 3
  • 1
  • 1
    Please provide a reproducible example (e.g. via `dput(data)`). What have you tried already? None of this seems to be using ggplot2. – Robin Gertenbach Aug 31 '21 at 15:14
  • See the examples [here](https://ggplot2.tidyverse.org/reference/position_stack.html). You probably need something like `... + geom_bar(position = 'fill')` – nniloc Aug 31 '21 at 15:17
  • 2
    You've tagged ggplot2, but you aren't actually using it. In addition to the [reproducible example](https://stackoverflow.com/q/5963269/5325862) guidance, take a look again at the *minimal* part of [mcve]; there's likely way more code here than is actually needed for creating? debugging? the plot, which is what the question is about – camille Aug 31 '21 at 15:47
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Aug 31 '21 at 21:22

1 Answers1

0

Let dummy is data you give as picture. Then,

dummy <- dummy %>% filter(Q71 != -99)
colnames(dummy) <- c("concern about home price", "concern about jobs", "concern about unemployment","concern about importation", "concern about inflation")

dummy %>% 
  reshape2::melt(value.name = "response", 
                 measure.var = c("concern about home price", 
                                 "concern about jobs", 
                                 "concern about unemployment",
                                 "concern about importation",
                                 "concern about inflation")) %>%
  group_by(variable, response) %>%
  summarise(n = n()/ 29) %>%
  ungroup %>%
  mutate(response = factor(response, labels = c("Strongly Disagree", "Disagree", "Neither Agree nor Disagree", "Agree", "Strongly Agree"), ordered = T)) %>%
  ggplot(aes(fill = response, y = n, x = variable)) +
  geom_bar(position = "fill", stat = "identity", width = 0.2) +
  coord_flip() + scale_fill_manual(values = c("steelblue", "yellow", "grey", "orange", "darkblue")) +
  theme_minimal()

result is like

stacked bar plot

Park
  • 14,771
  • 6
  • 10
  • 29