Visualizzazione post con etichetta basic. Mostra tutti i post
Visualizzazione post con etichetta basic. Mostra tutti i post

mercoledì 2 febbraio 2011

giovedì 7 gennaio 2010

Scatter plot with 4 axes labels and grid

Ravi from this post (via Revolutions blog) wanted to check the code that produces the left panel of the Figure 3 from this article taken from the current issue of the R Journal. Below my attempt to reproduce the plot:



rv <- seq(1.3, 2.9, .1)
rv <- rv[-grep("1.6", rv)] # remove R version 1.6
pckg.num <- c(110,129,162,219,273,357,406,548,647,739,911,1000,1300,1427,1614,1952)
rv.dates <- c("2001-6-21", "2001-12-17","2002-06-12","2003-05-27",
"2003-11-16","2004-06-05","2004-10-12","2005-06-18","2005-12-16", "2006-05-31",
"2006-12-12","2007-04-12","2007-11-16","2008-03-18","2008-10-18","2009-09-17")
pckg.fit <- lm(pckg.num~rv)
png("CRAN_packages.png")
par(mar=c(7, 5, 5, 3), las=2)
plot(as.POSIXct(rv.dates), pckg.num, xlab="",ylab="",col="red", log="y", pch=19, axes=F)
axis.POSIXct(1, 1:16, rv.dates, format="%Y-%m-%d")
mtext("Date", side=1, line=5, las=1)
axis(2, at=c(100,200,300,400,500,600,800,100,1200,1500,2000))
mtext("Number of CRAN Packages", side=2, line=3, las=3)
axis.POSIXct(3, rv.dates, rv.dates, labels=as.character(rv))
mtext("R Version", side=3, line=3, las=1)
axis(4, pckg.num)
abline(v=as.POSIXct(rv.dates), col="lightgray", lty="dashed")
abline(h=pckg.num, col="lightgray", lty="dashed")
box()
abline(lm(log10(pckg.num)~as.POSIXct(rv.dates)), col="red")
dev.off()

lunedì 15 settembre 2008

Fitting text under a plot

This is, REALLY, a basic tip, but, since I struggled for some time to fit long labels under a barplot I thought to share my solution for someone else's benefit.

As you can see (first image) the labels can not be displayed entirely:

counts <- sample(c(1000:10000),10)
labels <-list()
for (i in 1:10) { labels[i] <- paste("very long label number ",i,sep="")}
barplot( height=counts, names.arg=labels, horiz=F, las=2,col="lightblue", main="Before")


The trick to fit text of whatever dimension is to use the parameter mar to control the margins of the plot.

from ?par:

'mar' A numerical vector of the form 'c(bottom, left, top, right)'
which gives the number of lines of margin to be specified on
the four sides of the plot. The default is 'c(5, 4, 4, 2) + 0.1'.


op <- par(mar=c(11,4,4,2)) # the 10 allows the names.arg below the barplot
barplot( height=counts, names.arg=labels, horiz=F, las=2,col="skyblue", main="After")
rm(op)

lunedì 26 novembre 2007

This is one of that tiny function that help you to save yourself a lot of time! I thank the list for the suggestion!

library(gdata)
?keep
data(women, cars)
keep(cars)
## To remove all objects except cars, run:
## keep(cars, sure=TRUE)

giovedì 9 agosto 2007

R package installation and administration

A short list of basic but useful commands for managing
the packages in R:

# install a package
install.packages("ROCR")
# visualize package version
package_version("pamr")
# update a package
update.packages("Cairo")
# remove a package
remove.packages("RGtk2")