Dear lettice gurus,
the code below works just fine to produce a dotplot. However, I am not
successful changing the color of the lines in the legend (auto.key).
If I add col=..., it only changes the color of the letters in the
legend, but not the color of the lines.
Really appreciate any advice on that!
Dimitri
library(lattice)
d=data.frame(xx=c(2.2,2.1,3.3),yy=c(0.1,0.2,0.3),zz=c(2.5,2.0,1.8))
d[[2]]<-as.factor(d[[2]])
dotplot(c(d[[1]],d[[3]])~rep(d[[2]],2),groups=rep(c("Group 1","Group
2"),each=nrow(d)),main=list("Chart Title",cex=1),
xlab=list("Title for X",cex=.9,font=2),
ylab=list("Title for Y",cex=.9,font=2),
auto.key = list(space = "top", points = T, lines = T,cex=.9),
panel = function(y,x,...){
panel.grid(h = -1, v = -1, col = "gray", lty ="dotted", ltx="dotted")
panel.superpose(x,y,..., type="b",col.line=c("blue","red"),pch=20,cex=1.3,
col=c("blue","red"),bg=c("blue","red"),lwd=2)
ltext(x, y, labels=round(y,3),
cex=.8,col="black",font=2,adj=c(-0.2,1)) # adds labels to those
circles # pos=2,
panel.abline(h=0)
}
)
Dimitri Liakhovitski
MarketTools, Inc.
Dimitri.Liakhovitski at markettools.com
the code below works just fine to produce a dotplot. However, I am not
successful changing the color of the lines in the legend (auto.key).
If I add col=..., it only changes the color of the letters in the
legend, but not the color of the lines.
I prefer to set pch and friends outside the panel function to
avoid clutter, and to set the parameters globally, thus forcing
my plots to be similar. This is a matter of taste, though.
On Windows, the key looks a bit ugly now.
Group 1 o ----
Group
2 o-----
I am not very happy that the lwd is not honored by the key.
Lines in lwd 2 (plot) and in lwd 1 (default key) do have a quite
different subjective color hue. Any way around this, Deepayan?
(Besides using ggplot2, as Hadley would argue ??)
Dieter
library(lattice)
d=data.frame(xx=c(2.2,2.1,3.3),yy=c(0.1,0.2,0.3),zz=c(2.5,2.0,1.8))
d[[2]]<-as.factor(d[[2]])
sp = trellis.par.get("superpose.line")
sp$col=c("blue","red")
trellis.par.set("superpose.line",sp)
dotplot(c(d[[1]],d[[3]])~rep(d[[2]],2),groups=rep(c("Group 1","Group
2"),each=nrow(d)),main=list("Chart Title",cex=1),
type="b",pch=20,cex=1.3,lwd=2,
xlab=list("Title for X",cex=.9,font=2),
ylab=list("Title for Y",cex=.9,font=2),
auto.key = list(space = "top", points = T, lines = T,cex=.9),
panel = function(y,x,...)
{
panel.grid(h = -1, v = -1, col = "gray", lty ="dotted", ltx="dotted")
panel.superpose(x,y,... )
ltext(x, y, labels=round(y,3),cex=.8,col="black",font=2,
adj=c(-0.2,1))
}
)
the code below works just fine to produce a dotplot. However, I am not
successful changing the color of the lines in the legend (auto.key).
If I add col=..., it only changes the color of the letters in the
legend, but not the color of the lines.
I prefer to set pch and friends outside the panel function to
avoid clutter, and to set the parameters globally, thus forcing
my plots to be similar. This is a matter of taste, though.
On Windows, the key looks a bit ugly now.
Group 1 o ----
Group
2 o-----
[That's because the code got wrapped around, breaking "Group 2".]
I am not very happy that the lwd is not honored by the key.
Lines in lwd 2 (plot) and in lwd 1 (default key) do have a quite
different subjective color hue. Any way around this, Deepayan?
For the typical situation where you want the same colors for lines and
points, I find the
par.settings = simpleTheme(col=..., lwd=...)
construct very useful. Also note the slightly different key:
dotplot(c(d[[1]],d[[3]])~rep(d[[2]],2),
groups=rep(c("Group 1","Group 2"), each=nrow(d)),
main=list("Chart Title",cex=1),
type="b",
par.settings = simpleTheme(col=c("blue","red"),
pch=20, cex=1.3, lwd=2),
auto.key = list(space = "top", points = FALSE,
lines = TRUE, type = "b"),
xlab=list("Title for X",cex=.9,font=2),
ylab=list("Title for Y",cex=.9,font=2),
panel = function(y,x,...) {
panel.grid(h = -1, v = -1,
col = "gray", lty ="dotted")
panel.superpose(x,y,... )
ltext(x, y, labels=round(y,3),cex=.8,col="black",font=2,
adj=c(-0.2,1))
})
For different colors for lines and points, you do need to be more
verbose, and change the line and symbol settings separately:
trellis.par.set(superpose.line = list(col=c("blue","red"), lwd = 2),
superpose.symbol = list(cex = 1.3, pch = 20),
reference.line = list(col = "gray", lty ="dotted"))
dotplot(c(d[[1]],d[[3]])~rep(d[[2]],2),
groups=rep(c("Group 1","Group 2"), each=nrow(d)),
main=list("Chart Title",cex=1),
type="b",
auto.key = list(space = "top", points = TRUE, lines = TRUE),
xlab=list("Title for X",cex=.9,font=2),
ylab=list("Title for Y",cex=.9,font=2),
panel = function(y,x,...) {
panel.grid(h = -1, v = -1)
panel.xyplot(x, y, ...)
ltext(x, y, labels=round(y,3),
cex=.8,col="black",font=2,
adj=c(-0.2,1))
})
-Deepayan
the code below works just fine to produce a dotplot. However, I am not
successful changing the color of the lines in the legend (auto.key).
If I add col=..., it only changes the color of the letters in the
legend, but not the color of the lines.
I prefer to set pch and friends outside the panel function to
avoid clutter, and to set the parameters globally, thus forcing
my plots to be similar. This is a matter of taste, though.
On Windows, the key looks a bit ugly now.
Group 1 o ----
Group
2 o-----
I am not very happy that the lwd is not honored by the key.
Lines in lwd 2 (plot) and in lwd 1 (default key) do have a quite
different subjective color hue. Any way around this, Deepayan?
(Besides using ggplot2, as Hadley would argue ??)
And here is a ggplot2 solution ;)
df <- data.frame(
x = rep(d[[2]], 2),
y = c(d[[1]], d[[3]]),
group = rep(c("Group 1", "Group 2"), each = 3)
)
ggplot(df, aes(x, y, group = group)) +
geom_point(aes(colour = group), size = 3) +
geom_line(aes(colour = group), size = 1.5) +
geom_text(aes(label = y, y = y + 0.02), vjust = 0, size = 4)
The legends are pretty good at figuring out exactly what should be
displayed, based on what you've used in the graphic.
Hadley