问题
I want to assign two alpha values in xyplot
panel function:
points with alpha= 0.3
and lines with alpha=1
.
Here is an example:
library(lattice)
library(sp)
data(meuse)
xyplot(elev~ copper,data=meuse,groups=factor(soil),grid = TRUE,scales=list(tck=c(1,0), x=list(cex=1.1), y=list(cex=1.1)),
auto.key = list(space = 'right',text=c("1", "2", "3")),
par.settings = list(superpose.symbol = list(pch =20, cex = 1,
col = c("#006837", "#41ab5d","#fd8d3c"))),
type = c("p", "smooth"),col.line =c("#006837", "#41ab5d","#fd8d3c"),lwd = 5,
panel = function(x, ...) {
panel.xyplot(x, ..., alpha = 0.3)
panel.lines(x, ..., alpha = 1)
})
回答1:
An hex value can be read as "#rrggbbaa"
where r = red, g = green, b = blue and a = alpha. Since the decimal values for opacity range from 0 to 255, in a traditional rgb notation; the value for 30% opacity would be round((256/100)*30) = 77
, and hexadecimal value for this is 4d
(there is a list with some examples here for reference and a conversion table dec - hex can be found here).
Therefore, you just need to add 4d
at the end of your hex code for the points color as:
col = c("#0068374d", "#41ab5d4d","#fd8d3c4d")
and remove
panel = function(x, ...) {
panel.xyplot(x, ..., alpha = 0.3)
panel.lines(x, ..., alpha = 1)
}
来源:https://stackoverflow.com/questions/50121784/add-alpha-value-to-lattice-xyplot-panel-function