我写了一个R代码,提供了Ggplot图像,如图所示。我需要一些有关“是”,“否”和“也许”列的可自定义颜色代码的帮助。
更准确地说,我想根据“是”,“否”和“也许”的回答来更改每个问题出现的颜色。
我尝试了ggplot的基础知识。但是,我无法使颜色更具可定制性。
TABLE_NAME_ID
ggplot应该提供我确定的颜色。例如,第一季度中的“是”可能是绿色。但是,正如我所指出的,Q3中的“是”在颜色上变为“灰色”。这可能吗?
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()
格式的数据。
dput
答案 0 :(得分:1)
You can set color using scale_fill_manual or scale_color_manual depending on what you want. There you can specify the hex code for the color you want. If you name the list like I have you don't have to guess the legend order either.
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() +
scale_fill_manual( values = c("No" = "#ff0000","Yes" = "#00ff00", "Maybe" = "#0000ff") )