Remove legend ggplot 2.2

from r cookbook, where bp is your ggplot: Remove legend for a particular aesthetic (fill): bp + guides(fill=”none”) It can also be done when specifying the scale: bp + scale_fill_discrete(guide=”none”) This removes all legends: bp + theme(legend.position=”none”)

aggregate methods treat missing values (NA) differently

Good question, but in my opinion, this shouldn’t have caused a major debugging headache because it is documented quite clearly in multiple places in the manual page for aggregate. First, in the usage section: ## S3 method for class ‘formula’ aggregate(formula, data, FUN, …, subset, na.action = na.omit) Later, in the description: na.action: a function … Read more

How to sort a character vector where elements contain letters and numbers?

Try mixedsort from the “gtools” package: > # install.packages(“gtools”) ## Uncomment if not already installed > library(gtools) > mixedsort(cf) [1] “V51” “V108” “V116” “V120” “V155” “V217” “V327” “V440” “V446” “V457” “V477” If you don’t want to use mixedsort (not sure why one wouldn’t), and if your vector has a pretty consistent pattern (eg letters followed … Read more

Fast pairwise simple linear regression between variables in a data frame

Some statistical result / background (Link in the picture: Function to calculate R2 (R-squared) in R) Computational details Computations involved here is basically the computation of the variance-covariance matrix. Once we have it, results for all pairwise regression is just element-wise matrix arithmetic. The variance-covariance matrix can be obtained by R function cov, but functions … Read more

Is there a more elegant way to convert two-digit years to four-digit years with lubridate?

Here is a function that allows you to do this: library(lubridate) x <- mdy(c(“1/2/54″,”1/2/68″,”1/2/69″,”1/2/99″,”1/2/04”)) foo <- function(x, year=1968){ m <- year(x) %% 100 year(x) <- ifelse(m > year %% 100, 1900+m, 2000+m) x } Try it out: x [1] “2054-01-02 UTC” “2068-01-02 UTC” “1969-01-02 UTC” “1999-01-02 UTC” [5] “2004-01-02 UTC” foo(x) [1] “2054-01-02 UTC” “2068-01-02 … Read more