本文介绍了在R中创建轮廓线或百分位线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从此矩阵创建以下轮廓图。但是,我还要绘制一个曲线图,该曲线图显示由我选择的轮廓线(无填充轮廓),代表0.50(黑色),0.25和0.75(红色)的值。
该方法的帮助将不胜感激。

I am creating the following contour graph from this matrix. I would like to, however, have also a plot which displays chosen by me contour lines (without filled contour) representing values of 0.50 (in black) and 0.25 and 0.75 (in red).Help on the method will be much appreciated.

B = matrix(c(0.158,0.182,0.306,0.46,0.601,0.77,0.796,0.832,
             0.174,0.216,0.373,0.58,0.769,0.862,0.907,0.929,
             0.164,0.262,0.442,0.73,0.886,0.93,0.949,0.976,
             0.171,0.218,0.509,0.791,0.915,0.972,0.987,0.992,
             0.174,0.288,0.563,0.848,0.961,0.993,0.998,1,
             0.185,0.288,0.618,0.89,0.976,0.993,0.996,1,
             0.215,0.317,0.667,0.911,0.988,0.999,0.999,0.999,
             0.199,0.328,0.685,0.957,0.994,1,1,1,
             0.231,0.362,0.741,0.953,0.998,1,1,1,
             0.237,0.373,0.782,0.976,0.999,1,1,1,
             0.25,0.398,0.799,0.974,0.997,1,1,1,
             0.26,0.443,0.855,0.989,0.999,1,1,1,
             0.272,0.478,0.868,0.994,1,1,1,1,
             0.256,0.487,0.91,0.996,1,1,1,1,
             0.268,0.508,0.933,1,1,1,1,1),
             nrow=15, ncol=8,byrow=T) 


# Change column names
nv <- c(20,30,40,50,60,70,80,90,100,110,120,140,160,180,200)
location <- c(0,1,2,3,4,5,6,7) 
rownames(B) <- nv
colnames(B) <- location


# Create levels so they can be uniform accross all the images
b <- seq(0,1,0.05)

# Make levels that cover both ranges of z values
lvls <- pretty(range(b),20)

# Set up axis labels
matrix.axes <- function(data) {
        # Do the rows, las=2 for text perpendicular to the axis
        x <- (1:dim(data)[1] - 1) / (dim(data)[1] - 1);
        axis(side=1, at=x, labels=rownames(data), las=2);
        # Do the columns
        x <- (1:dim(data)[2] - 1) / (dim(data)[2] - 1);
        axis(side=2, at=x, labels=colnames(data), las=2);
}


filled.contour(B, plot.axes=matrix.axes(B),
               plot.title=title(main=paste("Graph"),
                                xlab='Sample number',ylab='Distance'),
               color.palette=colorRampPalette(c('white','blue','yellow','red','darkred')),
               levels=lvls)

以上是我目前所拥有的轮廓,但想将其更改为带有分别代表0.50(黑色),0.25和0.75的轮廓线的图形(红色)值(总共3行)。我正在标记ggplot2,因为那里可能有解决方案?

Filled contour above is what I have at the moment but want to change it to graph with distinct contour lines representing 0.50 (in black) and 0.25 and 0.75 (in red) values instead (3 lines total). I am tagging ggplot2 as there might be solution there?

推荐答案

使用轮廓线函数:

contour(B, levels = c(0.25, 0.5, 0.75), col = c(2, 1, 2), axes = F,
        xlab = 'Sample number', ylab = 'Distance')
axis(1, at = seq(0, 1, length= length(nv)), labels = nv)
axis(2, at = seq(0, 1, length= length(location)), labels = location)

这篇关于在R中创建轮廓线或百分位线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 15:45