I'm making a Likert plot 5 questions with 7 answer options "Completely Agree", "Agree", "Somewhat Agree", "Neither Agree nor Disagree", "Somewhat Disagree", "Disagree", "Completely Disagree", and an additional "Don't Know/No Opinion" Option. I really want to have the graph graded blue to red and have the "Don't Know/No Opinion" option separately in grey on the side of the graph. I've tried using the code suggested on here for a similar looking graph:
Likert plot with neutrals held aside
but I keep getting this warning message, making me think that the old code has deprecated... (and I don't know how I should write it correctly!)
Warning message:
There was 1 warning in `mutate()`.
ℹ In argument: `across(VarHeadings, .fns = ~as.factor(.))`.
Caused by warning:
! Using an external vector in selections was deprecated in tidyselect 1.1.0.
ℹ Please use `all_of()` or `any_of()` instead.
# Was:
data %>% select(VarHeadings)
# Now:
data %>% select(all_of(VarHeadings))
See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
My current code (for context, 8 is the code that I have assigned for a "Don't Know/No Opinion" answer
LikertTest <- read.csv("Likert_Test_Code_2.csv", header = TRUE, sep = ",")
#making fake data so at least every question is answered at least once in each category (to appease R!)
#Need to have the same number of "fake participants" as responses in the line below (8 point questions in my case)
Study_ID <- c("99991", "99992", "99993", "99994", "99995", "99996", "99997", "99998")
Q34_1 <- c(1, 2, 3, 4, 5, 6, 7, 8)
Q34_2 <- c(1, 2, 3, 4, 5, 6, 7, 8)
Q34_3 <- c(1, 2, 3, 4, 5, 6, 7, 8)
Q34_4 <- c(1, 2, 3, 4, 5, 6, 7, 8)
Q34_5 <- c(1, 2, 3, 4, 5, 6, 7, 8)
fake <- data.frame(Study_ID, Q34_1, Q34_2, Q34_3, Q34_4, Q34_5)
#Join fake data and convert variables to factor
LikertTest2 <- rbind(LikertTest, fake)
LikertTest2$Q34_1_f <- as.factor(LikertTest2$Q34_1)
LikertTest2$Q34_2_f <- as.factor(LikertTest2$Q34_2)
LikertTest2$Q34_3_f <- as.factor(LikertTest2$Q34_3)
LikertTest2$Q34_4_f <- as.factor(LikertTest2$Q34_4)
LikertTest2$Q34_5_f <- as.factor(LikertTest2$Q34_5)
#Attach factor levels
factor_levels <- c("Completely Agree","Agree", "Somewhat Agree", "Neither Agree nor Disagree", "Somewhat Disagree", "Disagree", "Completely Disagree", "Don't Know/No Opinion")
levels(LikertTest2$Q34_1_f) <- factor_levels
levels(LikertTest2$Q34_2_f) <- factor_levels
levels(LikertTest2$Q34_3_f) <- factor_levels
levels(LikertTest2$Q34_4_f) <- factor_levels
levels(LikertTest2$Q34_5_f) <- factor_levels
#removing fake data
#in code I nicked I have changed the names: LikertTest = survey1 and LikertTest2 = survey2 and LikertTest3 = survey3
nrow(LikertTest2)
LikertTest3 <- subset(LikertTest2, Study_ID < 99991)
nrow(LikertTest3)
#Making sure that it only plots the columnns of data that you actually want to use
colnames(LikertTest3)
LikertTest4 <- LikertTest3[,7:11]
colnames(LikertTest4)
#Naming columns
VarHeadings <- c("Regular disease screening is necessary for ALL species of reptiles and amphibians.","Regular disease screening is ONLY necessary for amphibians.","Regular disease screening is ONLY necessary for SOME species of reptiles and amphibians.","Disease screening is too expensive to do regularly.","I don't know where to find information on what diseases my animals could get.")
names(LikertTest4) <- VarHeadings
colnames(LikertTest4)
#Make the plot
p <- likert(LikertTest4)
a <- likert.bar.plot(p, legend.position = "right", text.size =4 ) + theme(text = element_text(size = rel(10)),axis.text.y = element_text(size = rel(10))) + theme_update(legend.text = element_text(size = rel(20))) + theme_classic()
plot(a)
Test_Graph<- plot(a)
#To make plot graded blue to red - (have avoided green due to colour blindness)
library("RColorBrewer")
colourupdate_Test_Graph <- Test_Graph + scale_fill_manual(values = brewer.pal(n=8,"RdYlBu"), breaks = c("Completely Disagree", "Disagree", "Somewhat Disagree", "Neither Agree nor Disagree", "Somewhat Agree", "Agree","Completely Agree", "Don't Know/No Opinion")) #order and color the likert boxes
colourupdate_Test_Graph
#reverse plot legends
legend_update_Test_Graph <- colourupdate_Test_Graph + guides(fill = guide_legend(title="Responses", reverse = TRUE))
#increase size of y-axis labels and legend labels
legend_update_Test_Graph_2 <-legend_update_Test_Graph + theme(axis.text=element_text(size=12)) + theme(legend.text=element_text(size=12))
legend_update_Test_Graph_2
#adding axis labels and title
Final_Graph<- legend_update_Test_Graph_2 + labs(title = "Keeper Opinions on Disease Screening in Reptiles and Amphibians")+ theme(plot.title = element_text(size=22))
#This gets me the graph I want except for sorting out the Don't Know option, so tried the code form the link here, but couldn't get it working:
levels(dftest$LikertTest4)
Optns <- c("Completely Agree", "Agree", "Somewhat Agree", "Neither Agree nor Disagree", "Somewhat Disagree", "Disagree", "Completely Disagree", "Don't Know/No Opinion")
dftest <- tibble("Regular disease screening is necessary for ALL species of reptiles and amphibians." = sample(Optns, 100, replace = T),
"Regular disease screening is ONLY necessary for amphibians." = sample(Optns, 100, replace = T),
"Regular disease screening is ONLY necessary for SOME species of reptiles and amphibians." = sample(Optns, 100, replace = T),
"Disease screening is too expensive to do regularly." = sample(Optns, 100, replace = T),
"I don't know where to find information on what diseases my animals could get." = sample(Optns, 100, replace = T)) %>%
mutate(across(VarHeadings, .fns = ~as.factor(.))) %>%
mutate(across(VarHeadings, .fns = ~ordered(., levels = Optns)))
#I've fiddled around a lot, but still can't get it working. Someone has also suggested "If that doesn't work, you can always do the old fashioned make two plots: one likert scale of the data and one order bar chart of the neutrals and use trellis or grid to place them next to each other" - but I also don't know how to do that! I also struggle to follow code when there is piping (as I'm a bit of a newbie) which doesn't help.
Any help gratefully recieved! Thanks :)