Tuesday, October 28, 2014

Treemaps in R

Following the directions from: http://flowingdata.com/2010/02/11/an-easy-way-to-make-a-treemap/

I learned:
  • data <- read.csv("http://datasets.flowingdata.com/post-data.txt")install.packages("portfolio")
  • library(portfolio)
  • map.market(id=data$id, area=data$views, group=data$category, color=data$comments, main="FlowingData Map")
So basically, I learned how to read in data from the Flowing Data website, how to install (and re-install--> dev.off() ) packages, and then the Treemap tutorial worked. Pretty awesome. 

This is the longer version of what happened:

> map.market(id=data$id, area=data$views, group=data$category, color=data$comments, main="FlowingData Map")

Hit <Return> to see next plot: 
Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : 
  invalid graphics state
> dev.off()
null device 
          1 
> data <- read.csv("http://datasets.flowingdata.com/post-data.txt")
> install.packages("portfolio")
Error in install.packages : Updating loaded packages
> library(portfolio)
> map.market(id=data$id, area=data$views, group=data$category, color=data$comments, main="FlowingData Map")

Restarting R session...

Loading required package: sp
> install.packages("portfolio")
trying URL 'http://cran.rstudio.com/bin/macosx/contrib/3.1/portfolio_0.4-6.tgz'
Content type 'application/x-gzip' length 1413591 bytes (1.3 Mb)
opened URL
==================================================
downloaded 1.3 Mb

Now to edit a CSV file with data that is interesting.



Making a colorwheel in R

Making a colorwheel in R

Here is the demo to refer to.


> title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3)

> title(xlab = "(Use this as a test of monitor linearity)",
+       cex.lab = 0.8, font.lab = 3)

> ## We have already confessed to having these.  This is just showing off X11
> ## color names (and the example (from the postscript manual) is pretty "cute".
>
> pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)

> names(pie.sales) <- c("Blueberry", "Cherry",
+      "Apple", "Boston Cream", "Other", "Vanilla Cream")

> pie(pie.sales,
+     col = c("purple","violetred1","green3","cornsilk","cyan","white"))

Now let's try changing the sizes.

Making a plot in R

Making a plot in R
First I had to figure out demos.
> demo(stats)
Error in demo(stats) : No demo found for topic ‘stats’
> demi(memisc)
Error: could not find function "demi"
> demo(memisc)
Error in demo(memisc) : No demo found for topic ‘memisc’
> demo(graphics)

Turns out, you can only call demo() on a package you have installed.
Well then.
>demo(graphics)

This is what R gave me then:

> abline(h = 0, col = gray(.90))

> lines(x, col = "green4", lty = "dotted")

> points(x, bg = "limegreen", pch = 21)

> title(main = "Simple Use of Color In a Plot",
+       xlab = "Just a Whisper of a Label",
+       col.main = "blue", col.lab = gray(.8),
+       cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)

> ## A little color wheel. This code just plots equally spaced hues in
> ## a pie chart. If you have a cheap SVGA monitor (like me) you will
> ## probably find that numerically equispaced does not mean visually
> ## equispaced.  On my display at home, these colors tend to cluster at
> ## the RGB primaries.  On the other hand on the SGI Indy at work the
> ## effect is near perfect.
>
> par(bg = "gray")

> pie(rep(1,24), col = rainbow(24), radius = 0.9)

So now I am going to try to make my own plot.