问题
I am using corrplot
to create a correlation heatmap, but I don't like the default legend - it is too big. So I was trying to use the colorlegend()
to add the legend after I create the plot (and disable the default legend with cl.pos="n"
).
Only problem is that I can't figure out how to change the position of the legend - it ends up on the lower left. Ideally, I could place it on the top right, but I looked through the options for colorlegend
and plot and can't figure this out.
For example:
# load libraries and create color scale
library(corrplot)
library(RColorBrewer)
scalebluered <- colorRampPalette(brewer.pal(8, "RdBu"))(8)
# get data into correlation matrix
data(mtcars)
cars.matrix <- as.matrix(mtcars[c(2:8)])
cars.corr <- cor(cars.matrix)
# plot it
corrplot(cars.corr, method="shade", shade.col=NA, tl.col="black",
tl.srt=45, addgrid.col="black", type="lower", diag=FALSE, cl.pos="n")
# add legend
colorlegend(scalebluered, c(seq(-1,1,.25)), align="l", vertical=TRUE, addlabels=TRUE)
output: http://i42.tinypic.com/14wsqc0.png
回答1:
Months later, I revisit this and figure it out.. just add xlim and ylim to define the dimensions of the legend.. super easy!
colorlegend(xlim=c(10,15), ylim=c(10,15), scalebluered, c(seq(-1,1,.25)), align="l", vertical=TRUE, addlabels=TRUE)
回答2:
like this? changing cl.pos="n" to "r"
corrplot(cars.corr, method="shade", shade.col=NA, tl.col="black",
tl.srt=45, addgrid.col="black", type="lower", diag=FALSE, cl.pos="r")
来源:https://stackoverflow.com/questions/19773819/how-to-place-colorlegend-corrplot-in-graphic