how to place colorlegend (corrplot) in graphic

谁说胖子不能爱 提交于 2019-12-12 09:49:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!