r - geom_text() 中超过 1 种颜色

标签 r ggplot2

我有这个数据:

data <- structure(list(Cod.projeto = c(7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), Nome.projeto = c("ESCOLAS INFANTIS", "ESCOLAS INFANTIS", "ESCOLAS INFANTIS", 
"ESCOLAS INFANTIS", "ESCOLAS INFANTIS", "ESCOLAS INFANTIS", "ESCOLAS INFANTIS", 
"ESCOLAS INFANTIS"), Secretarua = c("SMED", "SMED", "SMED", "SMED", 
"SMED", "SMED", "SMED", "SMED"), Comp = structure(c(1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L), .Label = c("28/02/2013", "01/03/2013"), class = c("ordered", 
"factor")), Estado = c("Criadas", "Criadas", "Providas", "Providas", 
"Criadas", "Criadas", "Providas", "Providas"), Categoria = c("A/G", 
"A/G", "A/G", "A/G", "B", "B", "B", "B"), Vagas = c(67, 67, 63, 
63, 124, 124, 73, 72)), .Names = c("Cod.projeto", "Nome.projeto", 
"Secretarua", "Comp", "Estado", "Categoria", "Vagas"), row.names = c(NA, 
-8L), class = "data.frame")

而这个 ggplot2()条形图:
require(ggplot2)
ggplot(data, aes(x=Categoria, y=Vagas, fill=relevel(factor(Estado),'Providas'))) +   
  geom_bar(position='dodge', stat='identity') +
  ggtitle(substitute(atop(titulo, atop(subtitulo)),list(titulo=paste(data[1,1:2],collapse=' - '),subtitulo=data[1,3]))) +
  theme(legend.position = "bottom") +
  xlab("Categoria") +
  ylab("Vagas") +
  scale_fill_grey('') +
  theme(panel.background = element_rect(fill='white'),panel.grid.major = element_line(colour = "gray70", linetype = 2)) +
  geom_text(aes(x=Categoria, y=Vagas/2, label = sprintf("%1.0f", Vagas)),position = position_dodge(height=1,width=1), size=4, colour='white') +
  facet_wrap( ~ Comp) 

结果:My plot

我只想在 ligth 灰色列(类别 = Criadas)中更改文本的颜色。我已经尝试在 colour 中使用两个值参数,但它不起作用。我所做的最接近的结果是当我添加 colour=relevel(factor(Estado),'Providas')) 时内aes() ,但文字是粉色和蓝色,并添加了一个我不想要的新图例。

最佳答案

如果您使用 colour=relevel(factor(Estado),'Providas')),它应该可以工作调用,但添加
+ scale_color_manual(values =c('white', 'black'),guide="none")
黑色是你想要的任何颜色。

关于r - geom_text() 中超过 1 种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15209899/

相关文章:

r - 使用 coord_fixed 时如何为 ggplot2 中的整个背景着色

r - 合并部分匹配的字符串

r - R 是否忽略数据框中以点开头的变量名称扩展?

r - 查找给定特征值R的特征向量

r - 如何在ggplot2的顶部面板边框添加线条

r - ggplot2 & stat_ellipse : Draw ellipses around multiple groups of points

r - 如何在 r 中使用整洁的文本进行二元模型主题建模?

R - 根据每一行的函数获取矩阵的列索引

R:将条形图与填充有匹配条形颜色的线条连接起来

r - 更改 coord_polar ggplot 的半径