Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than 1 color in geom_text()

Tags:

r

ggplot2

I have this data:

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")

and this ggplot2() barplot:

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) 

Resulting in: My plot

I want change de colour of the text only in the ligth gray column (Categoria = Criadas). I've already tried use two values in colour parameter, but it didn't work. The nearest result I did was when I added colour=relevel(factor(Estado),'Providas')) inside aes(), but the text was pink and blue and added a new legend that I dont want.

like image 490
Rcoster Avatar asked Mar 04 '13 19:03

Rcoster


1 Answers

It should work if you use the colour=relevel(factor(Estado),'Providas')) call, but add

+ scale_color_manual(values =c('white', 'black'),guide="none")

where black is whatever color you want.

like image 63
alexwhan Avatar answered Oct 21 '22 10:10

alexwhan