How do I remove the significant asterisks from this code?

372 views
Skip to first unread message

Teena Stockert

unread,
Jun 10, 2015, 4:35:30 PM6/10/15
to davi...@googlegroups.com


Help!  I want to get rid of the red significant asterisks! 


I have highlighted the areas in the source code in yellow that I have been trying to manipulate to remove them but with no luck - I just mess everything up!



 

##Source Code

#' correlation matrix chart

#'

#' Visualization of a Correlation Matrix. On top the (absolute) value of the

#' correlation plus the result of the cor.test as stars. On bottom, the

#' bivariate scatterplots, with a fitted line

#'

#'

#' @param R data for the x axis, can take matrix,vector, or timeseries

#' @param histogram TRUE/FALSE whether or not to display a histogram

#' @param method a character string indicating which correlation coefficient

#'           (or covariance) is to be computed.  One of "pearson"

#'           (default), "kendall", or "spearman", can be abbreviated.

#' @param \dots any other passthru parameters into \code{\link{pairs}}

#' @note based on plot at

#' \url{http://addictedtor.free.fr/graphiques/sources/source_137.R}

#' @author Peter Carl

#' @seealso \code{\link{table.Correlation}}

###keywords ts multivariate distribution models hplot

#' @examples

#'

#' data(managers)

#' chart.Correlation(managers[,1:8], histogram=TRUE, pch="+")

#'

#' @export

chart.Correlation <-

function (R, histogram = TRUE, method=c("pearson", "kendall", "spearman"), ...)

{ # @author R Development Core Team

  # @author modified by Peter Carl

    # Visualization of a Correlation Matrix. On top the (absolute) value of the

    # correlation plus the result of the cor.test as stars. On botttom, the

    # bivariate scatterplots, with a fitted line

 

    x = checkData(R, method="matrix")

   

    if(missing(method)) method=method[1] #only use one

 

    # Published at http://addictedtor.free.fr/graphiques/sources/source_137.R

    panel.cor <- function(x, y, digits=2, prefix="", use="pairwise.complete.obs", method, cex.cor, ...)

    {

        usr <- par("usr"); on.exit(par(usr))

        par(usr = c(0, 1, 0, 1))

        r <- cor(x, y, use=use, method=method) # MG: remove abs here

        txt <- format(c(r, 0.123456789), digits=digits)[1]

        txt <- paste(prefix, txt, sep="")

        if(missing(cex.cor)) cex <- 0.8/strwidth(txt)

 

        test <- cor.test(x,y, method=method)

        # borrowed from printCoefmat

        Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,

                    cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),

                    symbols = c("***", "**", "*", ".", " "))

        # MG: add abs here and also include a 30% buffer for small numbers

        text(0.5, 0.5, txt, cex = cex * (abs(r) + .3) / 1.3)

        text(.8, .8, Signif, cex=cex, col=2)

    }

    f <- function(t) {

    dnorm(t, mean=mean(x), sd=sd.xts(x) )

    }

    hist.panel = function (x, ...) {

        par(new = TRUE)

        hist(x,

             col = "light gray",

             probability = TRUE,

             axes = FALSE,

             main = "",

             breaks = "FD")

        lines(density(x, na.rm=TRUE),

              col = "red",

              lwd = 1)

        #lines(f, col="blue", lwd=1, lty=1) how to add gaussian normal overlay?

        rug(x)

      }

    # Draw the chart

    if(histogram)

        pairs(x, gap=0, lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=hist.panel, method=method, ...)

    else

        pairs(x, gap=0, lower.panel=panel.smooth, upper.panel=panel.cor, method=method, ...)

}

##Plotting my data to chart.correlation

## Correlation matrix with p-values. See http://goo.gl/nahmV for documentation of this function

cor.prob <- function (X, dfr = nrow(X) - 2) {

  R <- cor(X, use="pairwise.complete.obs")

  above <- row(R) < col(R)

  r2 <- R[above]^2

  Fstat <- r2 * dfr/(1 - r2)

  R[above] <- 1 - pf(Fstat, 1, dfr)

  R[row(R) == col(R)] <- NA

  R

}

 

## Use this to dump the cor.prob output to a 4 column matrix

## with row/column indices, correlation, and p-value.

## See StackOverflow question: http://goo.gl/fCUcQ

flattenSquareMatrix <- function(m) {

  if( (class(m) != "matrix") | (nrow(m) != ncol(m))) stop("Must be a square matrix.")

  if(!identical(rownames(m), colnames(m))) stop("Row and column names must be equal.")

  ut <- upper.tri(m)

  data.frame(i = rownames(m)[row(m)[ut]],

             j = rownames(m)[col(m)[ut]],

             cor=t(m)[ut],

             p=m[ut])

}

 

## get the CRG biomass data

##Mydata

Crgdata = read.table("C:/ CRGbiomassteena_redo1.txt", header=TRUE)

## use the path that your data file is located at

## In order to see if R is reading datacrgdata

crgdata

## You should see your data

 

# correlation matrix

cor(crgdata)

 

# correlation matrix with p-values

cor.prob(crgdata)

 

# "flatten" that table

flattenSquareMatrix(cor.prob(crgdata))

 

# plot the data

chart.Correlation(crgdata)

 

CRGbiomassteena_redo1.txt

Erica Rettig

unread,
Jun 10, 2015, 4:40:00 PM6/10/15
to davi...@googlegroups.com
Just comment out:

#text(.8, .8, Signif, cex=cex, col=2)


you can also comment out the lines where Signif is created but it's not 100% necessary.

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--
Thanks,
Erica


Erica Rettig
PhD Candidate
Graduate Group in Ecology, UC Davis

Christine Stockert

unread,
Jun 10, 2015, 4:44:51 PM6/10/15
to davi...@googlegroups.com
Thanks Erica,  I have only been using R for a few months now (self taught) and am not familiar with the term "comment out"  Can you explain what this means? I have deleted the "Signif" from this list and "txt" from the line above it and it was not happy.

You received this message because you are subscribed to a topic in the Google Groups "Davis R Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/davis-rug/MUL7rEEZdac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to davis-rug+...@googlegroups.com.



--
Christine "Teena" Stockert
Staff Research Associate
University of California, Davis
Viticulture and Enology
One Shields Ave.
Davis, CA 95616
530-754-7144 lab
530-383-5466 cell

Teena Stockert

unread,
Jun 10, 2015, 4:46:55 PM6/10/15
to davi...@googlegroups.com
actually, I deleted out everything in yellow and I got all "1" in the upper triangle for the R values with red "1" superscripts.

Erica Rettig

unread,
Jun 10, 2015, 4:47:37 PM6/10/15
to davi...@googlegroups.com
When you put a pound sign (aka hastag aka #) in front of a line of code, it tells R to ignore that line. So this:


text(.8, .8, Signif, cex=cex, col=2)

will run and this:

#text(.8, .8, Signif, cex=cex, col=2)

will not.

Don't delete the txt line, you need it, it creates the R^2 numbers.

Christine Stockert

unread,
Jun 10, 2015, 4:51:53 PM6/10/15
to davi...@googlegroups.com
OMG beautiful..  I knew it had to be simple.  THANK YOU!

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to a topic in the Google Groups "Davis R Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/davis-rug/MUL7rEEZdac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Christine Stockert

unread,
Jun 10, 2015, 5:00:52 PM6/10/15
to davi...@googlegroups.com
Now, any idea how to export as a pdf?

pdf("crgscatter.pdf", par(family="Times"))
plot(chart.Correlation(crgdata))
dev.off()

does not work


--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to a topic in the Google Groups "Davis R Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/davis-rug/MUL7rEEZdac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Erica Rettig

unread,
Jun 10, 2015, 5:05:18 PM6/10/15
to davi...@googlegroups.com
Those are the correct commands. Just make sure the pdf line happens before any of the plotting and dev.off line happens at the very end. typing "getwd()" (no quotes) into the R prompt will tell you what directory the PDF went to.

Myfanwy Johnston

unread,
Jun 10, 2015, 5:14:11 PM6/10/15
to davi...@googlegroups.com
you can also knit it all in an rmarkdown document if you're familiar - "knit pdf" button in RStudio.

On Wed, Jun 10, 2015 at 2:05 PM, Erica Rettig <erica....@gmail.com> wrote:
Those are the correct commands. Just make sure the pdf line happens before any of the plotting and dev.off line happens at the very end. typing "getwd()" (no quotes) into the R prompt will tell you what directory the PDF went to.

--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.



--
Myfanwy Johnston
Ph.D Candidate, Animal Behavior
University of California at Davis
Biotelemetry Lab
Reply all
Reply to author
Forward
0 new messages