在ggplot2中使用`scale_fill_manual`手动着色图不起作用

Mik*_*ike 9 r ggplot2

我正在努力解决如何手动更改条形颜色的问题ggplot2.奇怪的是,我可以得到它的使用需要使用一个传说更复杂的格式,当工作scale_fill_manual和设置values,labels等,但创造一个不需要一个传说简单的图表时,我似乎无法得到它的工作.下面是一个示例数据框架,我用来dplyr获取百分比的步骤,以及我认为它应该如何工作的步骤ggplot2.我只想手动将条形颜色更改为红色,seagreen3和灰色.

任何帮助,将不胜感激.我也很想知道用于快速计算百分比的不同方法.我一直在使用滚边dplyr,但如果能看到编写代码的其他方法会很棒.

library(dplyr)
library(ggplot2)

Service <- c("Satisfied", "Dissatisfied", "Neutral", "Satisfied", "Neutral")
Service2 <- c("Dissatisfied", "Dissatisfied", "Neutral", "Satisfied", "Satisfied")

Services <- data.frame(Service, Service2)

ServicesProp <- Services %>%
                select(Service) %>% group_by(Service) %>% 
                summarise(count=n()) %>%
                mutate(percent = count / sum(count))

ggplot(ServicesProp, aes(x = Service, y = percent)) + 
    geom_bar(stat = "identity", position = "dodge") + 
    scale_fill_manual(values = c("red", "seagreen3", "grey"))
Run Code Online (Sandbox Code Playgroud)

Mis*_*ist 18

万一你不确定@baptise是什么意思:

ggplot(ServicesProp, aes(x = Service, y = percent, fill = Service)) + 
  geom_bar(stat = "identity", position = "dodge") + 
  scale_fill_manual(values = c("red", "grey", "seagreen3"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述