0

尝试通过创建自己的调色板来分配每种可变颜色,但有些颜色会混淆。关于我应该如何解决这个问题的任何想法?

cor.partidos <- c(
  `ps` = "#f71b75",
  `psd` = "#ef6438",
  `pcp-pev` = "#ff001d",
  `pan` = "#71af64",
  `outros` = "#f71b75",
  `nulos` = "#565557",
  `brancos` = "#aaa8ad",
  `l` = "#f71b75",
  `il` = "#f71b75",
  `ch` = "#393195",
  `cds-pp` = "#1192d8",
  `be` = "#b40020",
  `a` = "#f71b75") 

#test graph
bars <- ggplot(leg19, aes(x = partido, y = votos)) +
  geom_bar(stat="identity", 
           position="identity", 
           fill = cor.partidos) +
  geom_hline(yintercept = 0, size = 1, colour="#333333") +
  bbc_style() +
  theme(axis.text=element_text(size=10))+
  labs(subtitle = "Resultados Legislativas 2019",
       ylab = "votos")

酒吧

用 mwe 更新

如果托盘中的变量与数据帧的顺序相同,它将起作用,但如果您将其混合一下,它将不起作用。将其更改为aes(fill = cor.partidos)不起作用:(

test.pallet <- c(
  `pink` = "#f71b75",
  `orange` = "#ef6438",
  `green` = "#71af64",
  `red` = "#ff001d",
  `other pink` = "#f71b72")

test.datafrane <- data_frame(
  name = c("pink","orange","red","green","other pink"),
  value = c(1,2,3,4,5)
)
test.datafrane$value <- as.numeric(test.datafrane$value)

test.graph <- ggplot(test.datafrane, aes(x = name, y = value)) +
  geom_bar(stat="identity", 
           position="identity", 
           fill = test.pallet)
test.graph

测试图

带有 aes 的 test.graph(fill = test.pallet)

4

1 回答 1

1

正如我在评论中建议的那样,您可以通过将您的分类变量映射fill到 aes() 内部并利用scale_fill_manual

test.pallet <- c(
  `pink` = "#f71b75",
  `orange` = "#ef6438",
  `green` = "#71af64",
  `red` = "#ff001d",
  `other pink` = "#f71b72")

test.datafrane <- data.frame(
  name = c("pink","orange","red","green","other pink"),
  value = c(1,2,3,4,5)
)
test.datafrane$value <- as.numeric(test.datafrane$value)

library(ggplot2)

test.graph <- ggplot(test.datafrane, aes(x = name, y = value, fill = name)) +
  geom_bar(stat="identity", 
           position="identity") +
  scale_fill_manual(values = test.pallet)
test.graph

于 2021-01-17T17:00:29.503 回答