Skip to Content

How do I get colors to match in ggplot2 in multiple aesthetics?

I am currently making a ggplot that displays two sets of aesthethics - one geom_histogram and one geom_line. The levels of each of these (called "cellname"), which designate color, are matched in order in their respective data frames. However, the coloring for each aesthetic doesn't match up. See code below:

hists_ggplot <- ggplot(cells,aes(x=residuals,fill=cellname))+
geom_histogram(position="dodge")+
scale_x_continuous("Deviation from Mean Location (nm)",limits=c(-150,150))+
scale_y_continuous("Time Points",expand=c(0,0))+
theme_bw() +
opts(panel.grid.minor=theme_blank(), panel.grid.major=theme_blank(),panel.background=theme_blank())+
opts(panel.border=theme_blank(), axis.line=theme_segment())+
opts(axis.text.y = theme_text(size=13))+
opts(axis.text.x = theme_text(size=13))+
opts(axis.title.y = theme_text(size=13, angle=90))+
opts(legend.position="none")+
geom_line(data=cells_functions,aes(x=deviations,y=fits,color=cellname))

My thought in doing this was that the fill=cellname for the first aesthetic and the color=cellname for the later aesthetic would result in matching colors, but it does not. Is the problem that the palette being pulled from is not "reset" when geom_line is called? Or is it something else?

I would greatly appreciate any help.

Thank you!

-Jeremy