Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R lattice: How to adjust the legend marker line width?

Does anyone know how to adjust the marker line width in a lattice plot, ideally using auto.key? I need to adjust it independently of the line thickness in the plot, which I require to be quite thin. By default the line in the legend is also very thin, which makes it hard to tell the different series apart by color or line style. A moderately thick rectangle would be an ideal series marker in the legend.

x = seq(1,360*10,1)
y = sin(x*pi/180)
df = data.frame(x=x, y=y, id="x")
p.xy <- xyplot(y~x, groups=id, data=df, type="l", lwd=.1
              ,auto.key=list(lines=T, points=F))
print(p.xy)

Thanks!
Bryan

like image 341
Bryan Avatar asked Dec 09 '25 23:12

Bryan


1 Answers

Using lwd in the call to xyplot changes the width of the line in the plot but not in the legend; the legend uses the parameter settings, which can be changed using the par.settings parameter. This is usually the preferred way to change the line width in the plot, as then it changes in the legend as drawn by auto.key as well, but in your case, this behavior can be used to change it as desired for the legend, and then override it with lwd for the lines in the plot.

I prefer to grab the default theme and change it as needed and then pass the whole thing back to par.settings, but you actually only need to give par.settings the parts you want to change from the default.

mtheme <- standard.theme("pdf", color=TRUE)
mtheme$plot.line$lwd <- 5
mtheme$superpose.line$lwd <- 5
p.xy <- xyplot(y~x, groups=id, data=df, type="l", lwd=0.1
              ,auto.key=list(lines=T, points=F), par.settings=mtheme)
print(p.xy)
like image 156
Aaron left Stack Overflow Avatar answered Dec 12 '25 16:12

Aaron left Stack Overflow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!