0

My Data.
The input I receieve for my graphs are "Agree", "Disagree", "Somewhat Agree", and "Somewhat Disagree" in that order. The output I want to see graphed follows a "Agree", "Somewhat Agree", "Somewhat Disagree" and "Disagree" order. I have tried to also change the order in the excel table but nothing comes from that. ``

data_setReading20 <- read_excel("~/Desktop/Survey_Data1_ENGSPA(FIXED).xlsx")

my_data20 <- data_setReading20[, c("par_res20", "par_count20")]

ggplot(data = my_data20, aes(x = par_res20, y = par_count20, fill = par_res20)) +
  geom_bar(stat = "identity") + 
  geom_text(mapping = aes(label=par_count20), position = position_dodge(width = 0.9), cex = 2.5, vjust=-1) +    
  labs(fill = "Responses",
       x = "Youth Responses",
       y = "Youth Count",
       subtitle = "I want to come OR return to _ in person.",
       caption = "N = 312, youth responses to survey in English and Spanish") +
  scale_fill_manual(values = c("Agree" = "#FCD81F",
                               "Somewhat Agree" = "#F88B3B", 
                               "Somewhat Disagree" = "#F8543B",
                               "Disagree" = "#F8AA3B")) + 
  theme_bw()
Jrock2897
  • 11
  • 2
  • can you change the variable to a factor and then plot it? – Mike Jul 13 '21 at 15:07
  • Would that involve factoring my_data20? Since factor(my_data20) would give me an error for being different datatypes – Jrock2897 Jul 13 '21 at 15:20
  • you would need to share your data so I can help troubleshoot but if you can't I would suggest looking at the help page for factor with `?factor` – Mike Jul 13 '21 at 15:29
  • I just added the data in a picture, it is a very small amount of data so not too much. But the factoring process has me a bit confused honestly. – Jrock2897 Jul 13 '21 at 15:40
  • you have an answer below that would probably work, but in the future when sharing data try to use `dput(yourdata)` and copy that output into the question. – Mike Jul 13 '21 at 16:22
  • I usually like to set the factor levels of a variable as has already been suggested. However, another option is skipping this step and then manually setting the `limits` of the discrete x axis in your plot: `+ scale_x_discrete(limits = c("Agree", "Somewhat Agree", "Somewhat Disagree", "Disagree"))` – aosmith Jul 13 '21 at 17:11

1 Answers1

1

Convert the corresponding column to an ordered factor:

my_data20$par_res20 <- factor(my_data20$par_res20, 
    levels=c("Agree", "Somewhat Agree", "Somewhat Disagree", "Disagree"),
    ordered=T)
c0bra
  • 1,031
  • 5
  • 22
  • 1
    Just an FYI, ggplot2 will choose a different default color scheme for *ordinal* data vs categorical data (this always surprises me ;)). You don't have to make your data ordered/ordinal via `ordered = TRUE` to set the category order, although I could see it making sense in this case. Setting the factor levels in the order you want via `levels` without using `ordered = TRUE` will also set the order of the categories for plotting. – aosmith Jul 13 '21 at 17:07
  • @aosmith I generally think it is a better idea to correctly specify your data instead of just hoping it will be correctly processed. – c0bra Jul 14 '21 at 10:04