Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of density plots in ggplot2

Tags:

r

ggplot2

I've got a nifty pair of probability density functions I created with ggplot2 as follows:

require(ggplot2) set.seed(2) data <- rbind( data.frame(type="a", lr=rnorm(100)), data.frame(type="b", lr=rnorm(100,.5,1.2))) m <- ggplot(data, aes(x=lr))  m <- m + geom_density(aes(fill=factor(type)), size=2, alpha=.4)  m 

and that produces a nice plot:

enter image description here

However I would really like that plot to have different color shading. I can't seem to figure it out. I thought the following would work:

m + scale_colour_manual( values = c("red","blue")) 

But no luck. I also tried passing fill colors to the geom_density() call, to no avail.

How do I change the fill colors?

like image 801
JD Long Avatar asked Sep 09 '11 15:09

JD Long


People also ask

How do you change the color of a density plot in R?

Change density plot colors by groupsscale_color_manual() : to use custom colors. scale_color_brewer() : to use color palettes from RColorBrewer package. scale_color_grey() : to use grey color palettes.

How do you make a smooth density plot in R?

Frist of all, create a data frame. Load ggplot2 package and creating the density plot for the whole data. Create the density plot for the categories in the data frame by using col function.

What is density in ggplot2?

A density plot is a representation of the distribution of a numeric variable. It is a smoothed version of the histogram and is used in the same kind of situation. Here is a basic example built with the ggplot2 library. Density Section Density theory. Density plots are built in ggplot2 thanks to the geom_density geom.


1 Answers

Start kicking yourself:

m + scale_fill_manual( values = c("red","blue")) 

enter image description here

like image 107
Andrie Avatar answered Sep 18 '22 14:09

Andrie