Visualizing car brand choices in ggplot2

June 2, 2012
By

(This article was first published on R snippets, and kindly contributed to R-bloggers)

I always like to read new posts at chartsnthings as they always inspire me with new ideas for data visualization. Yesterday I have read an article on choices of car brands by members of parliament in Poland in Gazeta.pl. It contains a simple table graph created using Tableau that was interesting to replicate in GNU R.
The source data (cars.txt) contains club name each member of parliament belongs to and name of the car she owns. Here is the head of the data set:

cars <- read.table("cars.txt",
  head = T, sep = "\t"quote = "")
head(cars)
#   club                              brand
# 1   PO                         Citroen C8
# 2   PO Opel Astra IV Combi 1,4 Turbo 2011
# 3   PO                     Mercedes E 300
# 4   PO                   Chevrolet Blazer
# 5   PO                     Mercedes C 202
# 6   PO                         Mercedes G

In order to do the cross tabulation of clubs against car brands first I leave only brand name in the data, next I order both factors by categories count and plot the data:

library(ggplot2)
library(plyr)

# leave only car brand
cars$brand <- factor(sapply(cars$brand,
  function(x) { strsplit(as.character(x)," ")[[1]][1] }))

# order clubs and brands by counts
cars$club <- ordered(cars$club,
  names(sort(table(cars$club))))
cars$brand <- ordered(cars$brand,
  names(sort(table(cars$brand), decreasing = TRUE)))

# transform the data for plotting
scars <- ddply(cars, .(brand, club), .fun = nrow)

ggplot() +
  geom_point(data = scars,
    aes(x = brand, y = club, colour = log(V1)),
    shape=15, size = 4) +
  scale_colour_gradient(low = "#AFE9AF", high = "#0B280B") +
  opts(panel.background = theme_blank(),
    legend.position = "none",
    axis.title.x = theme_blank(),
    axis.title.y = theme_blank(),
    axis.text.x = theme_text(angle = -90),
    axis.text.y = theme_text(colour = "black"))

Here is the final plot:


To leave a comment for the author, please follow the link and comment on his blog: R snippets.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.