1

I have the following code:

library(ggplot2)
library(gridExtra)

data = data.frame(fit = c(9.8,15.4,17.6,21.6,10.8), lower = c(7.15,12.75,14.95,18.95,8.15), upper = c(12.44,18.04,20.24,24.24,13.44), factors = c(15,20,25,30,35), var = rep("Fator", 5))

gp <- ggplot(data, aes(x=factors, y=fit, ymax=upper, ymin=lower))
gp <- gp + geom_line(aes(group=var),size=1.2) + 
               geom_errorbar(width=.8, size=1, aes(colour='red')) + 
               geom_point(size=4, shape=21, fill="grey") +  
               labs(x = paste("\n",data$var[1],sep=""), y =paste("Values","\n",sep="")) + 
               theme(legend.position = 'none', axis.text = element_text(size = 11), plot.margin=unit(c(0.4,0.4,0.4,0.4), "cm"), axis.text.x  = element_text(angle=45, hjust = 1, vjust = 1)) +
               ylim((min(data$lower)), (max(data$upper)))

I want to change the line color after I have the ggplot object. I'm trying:

gp + scale_color_manual(values = "green")

but it change the error bar color and not the line color.

1)What should I do to change the line color?

2)How can I change the points color?

Thanks!

1 Answers1

0

Try this:

gp$layers[[1]] <- NULL
gp + geom_line(aes(group = var),color = "green",size = 1.2)

A similar technique should work for the points layer. Technique was dredged up from my memories of a similar question.

I just looked at the contents of gp$layers manually to see which was which. I presume that the order will be the order in which they appear in your code, but I wouldn't necessarily rely on that.

Community
  • 1
  • 1
joran
  • 169,992
  • 32
  • 429
  • 468
  • This worked out! I'm trying to create a function that you could edit the colors and shapes of any generic ggplot object. Now I just need a way to recognize the layers from the object and change the parameters. Thank you very much for your fast answer! – André Tavares Aug 28 '14 at 19:04
  • I found another way using your reasoning. I just modified it: gp$layers[[1]]$geom_params$color = "green" This way I will not lose the informations from the initial layer. – André Tavares Sep 02 '14 at 16:48