小编Joe*_*ass的帖子

R如何使用ggplot强调x轴?

如果我使用ggplot,那么x轴(y == 0)的水平线与y的任何其他值相同.我想强调一下这样一个事实:图的底部不是x轴,并且图中的x轴更高.我怎样才能做到这一点?

data.df <- data.frame(Plant = c("Plant1", "Plant1", "Plant1", "Plant2", "Plant2", "Plant2"), Type = c(1, 2, 3, 1, 2, 3), Axis1 = c(0.2, -0.4, 0.8, -0.2, -0.7, 0.1), Axis2 = c(0.5, 0.3, -0.1, -0.3, -0.1, -0.8))

ggplot(data.df, aes(x = Axis1, y = Axis2, shape = Plant, color = Type)) + geom_point(size = 5)
Run Code Online (Sandbox Code Playgroud)

axis r ggplot2

4
推荐指数
2
解决办法
1636
查看次数

如何规范化卷积图像?我是不是该?

我试图用卷积进行边缘检测.我想我需要在卷积后对图像进行标准化.

我正在使用此处指定的卷积矩阵:https: //en.wikipedia.org/wiki/Kernel_(image_processing)#Convolution

附件是一些r代码,源和输出图像......

require(jpeg)
myjpg <- readJPEG("mtg.jpg")

grayImg <- myjpg[,,1]+myjpg[,,2]+myjpg[,,3] # reduce to gray
grayImg <- grayImg/max(grayImg) # normalize
dim(grayImg)


convolve <- function(img, f){
    newimg <- img
    radius <- as.integer(nrow(f)/2)+1
    print(radius)
    for(i in c(1:nrow(img))){
        for(j in c(1:ncol(img))){
            f_sub <- f[c(max(1,radius-i+1):min(nrow(f),nrow(img)-i+radius)),c(max(1,radius-j+1):min(ncol(f),ncol(img)-j+radius))]
            img_sub <- img[c(max(1,i-radius+1):min(nrow(img),i+radius-1)),c(max(1,j-radius+1):min(ncol(img),j+radius-1))]
            wavg <- sum(as.vector(f_sub)*as.vector(img_sub))# / sum(as.vector(f_sub)) # todo, not sure about this division
            newimg[i,j] <- wavg
        }
    }
    return(newimg)
}

edgeFilter <- matrix(c(-1,-1,-1,-1,8,-1,-1,-1,-1), ncol = 3)

outimg <- convolve(grayImg,edgeFilter)

outimg <- outimg - min(outimg)
outimg …
Run Code Online (Sandbox Code Playgroud)

r image-processing

4
推荐指数
1
解决办法
3179
查看次数

R对于每个第三维,通过向量乘以3D阵列的第二维

当试图将一个数组的第一个维度乘以一个向量的每个索引乘以第二个维度时,我的数组被转换为一个矩阵,事情变得松散.我只能长时间做适当的乘法.

多么满口......

使用代码更容易解​​释......

Arr <- array(runif(10*5*3), dim = c(10,5,3))
dim(Arr)
Vect <- c(1:5)

Arr[,1,1] <- Arr[,1,1]*Vect[1]
Arr[,1,2] <- Arr[,1,2]*Vect[1]
Arr[,1,3] <- Arr[,1,3]*Vect[1]

Arr[,2,1] <- Arr[,2,1]*Vect[2]
Arr[,2,2] <- Arr[,2,2]*Vect[2]
Arr[,2,3] <- Arr[,2,3]*Vect[2]

Arr[,3,1] <- Arr[,3,1]*Vect[3]
Arr[,3,2] <- Arr[,3,2]*Vect[3]
Arr[,3,3] <- Arr[,3,3]*Vect[3]

Arr[,4,1] <- Arr[,4,1]*Vect[4]
Arr[,4,2] <- Arr[,4,2]*Vect[4]
Arr[,4,3] <- Arr[,4,3]*Vect[4]

Arr[,5,1] <- Arr[,5,1]*Vect[5]
Arr[,5,2] <- Arr[,5,2]*Vect[5]
Arr[,5,3] <- Arr[,5,3]*Vect[5]
Run Code Online (Sandbox Code Playgroud)

如何将其清理为一个命令?

arrays r

4
推荐指数
2
解决办法
725
查看次数

赋值运算符重载:返回void与返回引用参数

我在网上看到的一些赋值重载操作符示例如下所示:

#include <iostream>
using namespace std;

class Distance {
   private:
      int feet;             // 0 to infinite
      int inches;           // 0 to 12
   public:
      // required constructors
      Distance(){
         feet = 0;
         inches = 0;
      }

      Distance(int f, int i){
         feet = f;
         inches = i;
      }

      void operator = (const Distance &D ) { 
         cout << "assigning..." << endl;
         feet = D.feet;
         inches = D.inches;
      }

      // method to display distance
      void displayDistance() {
         cout << "F: " << feet << …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading assignment-operator

3
推荐指数
2
解决办法
3317
查看次数

通过枢转 Cholesky 分解生成具有秩亏协方差的多元正态 rv

我只是在拼命尝试让乔列斯基分解发挥作用,以模拟相关的价格变动。

我使用以下代码:

cormat <- as.matrix(read.csv("http://pastebin.com/raw/qGbkfiyA"))
cormat <- cormat[,2:ncol(cormat)]
rownames(cormat) <- colnames(cormat)
cormat <- apply(cormat,c(1,2),FUN = function(x) as.numeric(x))

chol(cormat)
#Error in chol.default(cormat) : 
#    the leading minor of order 8 is not positive definite

cholmat <- chol(cormat, pivot=TRUE)
#Warning message:
#    In chol.default(cormat, pivot = TRUE) :
#    the matrix is either rank-deficient or indefinite

rands <- array(rnorm(ncol(cholmat)), dim = c(10000,ncol(cholmat)))
V <- t(t(cholmat) %*% t(rands))

#Check for similarity
cor(V) - cormat  ## Not all zeros!

#Check the standard deviations
apply(V,2,sd) …
Run Code Online (Sandbox Code Playgroud)

random statistics r normal-distribution matrix

1
推荐指数
1
解决办法
841
查看次数

C++为什么非const数组声明不好?

如果我有以下两个陈述:

// OK
const int ARRAYSIZE = 5;
int x[ARRAYSIZE];

// NOT OK
int ARRAYSIZEBAD = 5;
int y[ARRAYSIZEBAD];
Run Code Online (Sandbox Code Playgroud)

我不用-pedantic-errors标志编译...为什么第二个例子是坏事?在什么情况下,最好使用new运算符进行动态分配?

c++ heap stack

-1
推荐指数
1
解决办法
187
查看次数