I have a text table with 5 columns. I want to plot 4 of the columns as individual density plots on the same plot. I can achieve this as below:
Code for the above plot:
library(ggplot2)
library(grid)
dat <- read.table(textConnection("
file low high avg lowest
102 4218.0 5437.0 4739.0 4723.0
103 4516.0 5765.0 5061.0 5036.0
104 4329.0 5554.0 4858.0 4838.0
107 4094.0 5261.0 4596.0 4578.0
108 4334.0 5569.0 4865.0 4846.0
109 4397.0 5596.0 4924.0 4896.0
110 4046.0 5257.0 4555.0 4547.0
"), header=TRUE)
x_low = dat$low
x_high = dat$high
x_avg = dat$avg
x_lowest = dat$lowest
plotter = ggplot() + geom_density(aes(x=x_low), colour="red", fill="red", alpha = .3, data=data.frame(dat$low))
plotter = plotter + geom_density(aes(x=x_high),colour="blue", fill="blue", alpha = .3, data=data.frame(dat$high))
plotter = plotter + geom_density(aes(x=x_avg), colour="green", fill="green", alpha = .3, data=data.frame(dat$avg))
plotter = plotter + geom_density(aes(x=x_lowest), colour="purple", fill="purple", alpha = .3, data=data.frame(dat$lowest))
plotter = plotter + xlim(c(2000,7000))
print(plotter)
I would now like to have a legend on the side of the plot. From my understanding I need to move the colour
inside the brackets of aes
I do this as follows:
library(ggplot2)
library(grid)
dat <- read.table(textConnection("
file low high avg lowest
102 4218.0 5437.0 4739.0 4723.0
103 4516.0 5765.0 5061.0 5036.0
104 4329.0 5554.0 4858.0 4838.0
107 4094.0 5261.0 4596.0 4578.0
108 4334.0 5569.0 4865.0 4846.0
109 4397.0 5596.0 4924.0 4896.0
110 4046.0 5257.0 4555.0 4547.0
"), header=TRUE)
x_low = dat$low
x_high = dat$high
x_avg = dat$avg
x_lowest = dat$lowest
plotter = ggplot() + geom_density(aes(x=x_low, colour="red", fill="red"), alpha = .3, data=data.frame(dat$low))
plotter = plotter + geom_density(aes(x=x_high, colour="blue", fill="blue"), alpha = .3, data=data.frame(dat$high))
plotter = plotter + geom_density(aes(x=x_avg, colour="green", fill="green"), alpha = .3, data=data.frame(dat$avg))
plotter = plotter + geom_density(aes(x=x_lowest, colour="purple", fill="purple"), alpha = .3, data=data.frame(dat$lowest))
plotter = plotter + xlim(c(2000,7000))
print(plotter)
This outputs:
The colours of each plot are now wrong (when compared to the first plot) as well as the labelling in the legend.
How can I:
- Correct the colouring
- Remove the dark outline of each density plot
- Correct the legend