如何在R中使用透视图添加颜色条 [英] How to add colorbar with perspective plot in R

查看:134
本文介绍了如何在R中使用透视图添加颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这很简单,但是经过数小时的搜索,我找不到使用R在persp图旁边添加颜色条的方法.有人可以帮忙吗?谢谢.

May be it is very simple to do, but after hours of searching I didn't able to find how to add colorbar beside the persp plot using R. Could anyone kindly help? Thanks.

persp(w_lb, w_dti, cm[[i]], 
        theta = -30, phi = 30, expand = 0.95, 
        col=color[facetcol], shade = 0.25, 
        ticktype = "detailed", border = NA, 
        xlab = "LB", ylab = "DT", zlab="CM", 
        zlim=c(0.0, 1.0)
)

推荐答案

可以使用fields包中的image.plot添加图例.使用来自?persp的示例:

It's possible to add a legend using image.plot in the fields package. Using an example from ?persp:

library(fields)

## persp example code
par(bg = "white")
x <- seq(-1.95, 1.95, length = 30)
y <- seq(-1.95, 1.95, length = 35)
z <- outer(x, y, function(a, b) a*b^2)
nrz <- nrow(z)
ncz <- ncol(z)
# Create a function interpolating colors in the range of specified colors
jet.colors <- colorRampPalette( c("blue", "green") )
# Generate the desired number of colors from this palette
nbcol <- 100
color <- jet.colors(nbcol)
# Compute the z-value at the facet centres
zfacet <- (z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz])/4
# Recode facet z-values into color indices
facetcol <- cut(zfacet, nbcol)
persp(x, y, z, col = color[facetcol], phi = 30, theta = -30, axes=T, ticktype='detailed')

## add color bar
image.plot(legend.only=T, zlim=range(zfacet), col=color)

通过@Marc_in_the_box进行颜色栏的范围由zfacet而不是z定义

EDIT thanks to @Marc_in_the_box: the range of the colorbar is defined by zfacet, not by z

这篇关于如何在R中使用透视图添加颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆