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:
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?
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.
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.
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.
Start kicking yourself:
m + scale_fill_manual( values = c("red","blue"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With