Hi there,
I am trying to plot the level of agreement with a variable by location. What has me stumped is the fact that respondents can select multiple locations (i.e. their place of business is located in several locations).
I've used summarise(sum)
on one location, but this method (see below) does not work when I add new locations.
Here is a sample dataset
data.frame(
Brisbane_CBD = c(0L,1L,
1L,0L,0L,0L,0L,0L,0L,
1L),
Greater_Bris = c(0L,0L,
0L,0L,0L,0L,0L,0L,0L,
1L),
Regional_City = c(1L,1L,
1L,0L,1L,1L,1L,1L,1L,
1L),
Rural_Town = c(0L,0L,
0L,1L,0L,0L,0L,0L,0L,
1L),
Virtual = c(0L,0L,
0L,0L,0L,0L,0L,0L,0L,
1L),
Equipped_for_future_changes = c("c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)",
"c(NA, NA, 4, 4, 3, 3, 4, 2, 3, 5)")
)
Here is the code I am using
library(ggplot2)
library(tidyr)
library(tibble)
library(dplyr)
library(likert)
library(RColorBrewer)
library(string)
lbs_DF <- c("Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree")
DF$Equipped_for_future_changes <- DF %>%
select("Equipped_for_future_changes")%>%
dplyr::mutate_if(is.numeric, factor, levels = 1:5, labels = lbs_DF)%>%
as.data.frame()
#> Error in DF %>% select("Equipped_for_future_changes") %>% dplyr::mutate_if(is.numeric, : could not find function "%>%"
head(DF)
#> Error in head(DF): object 'DF' not found
DF %>%
group_by(Equipped_for_future_changes) %>%
summarise(sum(Brisbane_CBD))
#> Error in DF %>% group_by(Equipped_for_future_changes) %>% summarise(sum(Brisbane_CBD)): could not find function "%>%"
Created on 2023-01-24 with reprex v2.0.2
This code produces the following result
Equipped_for_future_changes$Equipped_for_future_changes `sum(Brisbane_CBD)`
<fct> <int>
1 Strongly Disagree 1
2 Disagree 3
3 Neutral 7
4 Agree 19
5 Strongly Agree 11
6 NA 12
I need to do this (or something more elegant) for all locations simultaneously to plot all locations along the x-axis with the Equipped_for_future_changes filling the bar_plot or geom_col.
Thank you in advance for any help or advice you can provide.
Kind Regards
Aaron