I am creating an R script to produce a chart consisting of multiple lines. I have produced a basic chart, and have managed to smooth each of the plotted lines. However, I want to beautify the chart by doing the following:
-
Currently, each line is plotted as a smoothed line. I want to plot the smoothed line using some other line type e.g. dash-dot, dash-dash-dot etc.
-
I want to add a legend to the chart, and display a line (same color as the line it represents in the main chart), in the legend.
-
Specify font size for the main chart title, x and y
Here is what I have so far:
dat <- read.csv(filename, sep=',')
xvals <- dat$xvals
y1 <- dat$y1
y2 <- dat$y2
y3 <- dat$y3
y4 <- dat$y4
y5 <- dat$y5
y6 <- dat$y6
lo1 <- loess(y1~xvals)
lo2 <- loess(y2~xvals)
lo3 <- loess(y3~xvals)
lo4 <- loess(y4~xvals)
lo5 <- loess(y5~xvals)
lo6 <- loess(y6~xvals)
plot(xvals,y1, xlab='X label', ylab='Y label', type='n')
xl <- seq(min(xvals),max(xvals), (max(xvals) - min(xvals))/1000)
lines(xl, predict(lo1,xl), col='gray', lwd=1)
lines(xl, predict(lo2,xl), col='pink', lwd=1)
lines(xl, predict(lo3,xl), col='red', lwd=1)
lines(xl, predict(lo4,xl), col='cyan', lwd=1)
lines(xl, predict(lo5,xl), col='black', lwd=1)
lines(xl, predict(lo6,xl), col='green', lwd=1)
legend("topright", "(x,y)", pch=1, lty=c(1,1), # gives the legend appropriate symbols (lines)
, lwd=c(1,1),col=c("blue","red"), inset = .02)
How do I modify the code above to implement the requirements 1 – 3 above?
As an aside, I am aware of ggplot. I think I’ll stick with plot() – I find ggplot far too cryptic, puzzling and infuriating.
Since I had started work on an answer already:
Make up some data:
Create formulae, apply
loessandpredict, collapse the results into a matrix:Set parameters (it’s always worth going back and spending some more time reading
?par):Set up colo(u)rs and line types:
Create the plot: