I have written an R code that provides a Ggplot image as shown. I need some help with customisable colour codes for the "Yes", "No" and "Maybe" column.
More precisely, I would like to change colours that appear for each question based on the "Yes", "No" and "Maybe" responses.
I have tried the basics of ggplot. However, I am not able to make the colours more customisable then it already is.
library(dplyr)
library(ggplot2)
theme_set(theme_classic())
library(tidyverse)
data <- read.csv('data.csv', header = T, stringsAsFactors = F)
str(data)
data$stemmed <- factor(data$stemmed, levels=c("No", "Yes", "Maybe"))
data$QuestionNumber <- ordered(data$QuestionNumber, levels = c("Q1", "Q2", "Q3", "Q4", "Q5","Q6","Q7","Q8","Q9","Q10","Q11","Q12", "Q13", "Q14", "Q15", "Q16", "Q17"))
data$QuestionNumber = forcats::fct_rev(factor(data$QuestionNumber))
g <- ggplot(data, aes(QuestionNumber))
g + geom_bar(aes(fill=stemmed), width = 0.5) +
theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
labs(title="Histogram Plot") + coord_flip()
The ggplot should provide colours that I have determined. For example, the "Yes" in Q1 could be GREEN in colour. But, the "Yes" in Q3 becomes "GREY" in colour as I denote. Is this possible ?
Data in dput
format.
data <-
structure(list(QuestionNumber = structure(c(17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 15L, 15L,
15L, 15L, 15L), .Label = c("Q17", "Q16", "Q15", "Q14", "Q13",
"Q12", "Q11", "Q10", "Q9", "Q8", "Q7", "Q6", "Q5", "Q4", "Q3",
"Q2", "Q1"), class = c("ordered", "factor")), stemmed = structure(c(2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 2L, 2L,
3L, 2L, 3L), .Label = c("No", "Yes", "Maybe"), class = "factor")), row.names = c(NA,
20L), class = "data.frame")