Visualizing car brand choices in ggplot2

June 2, 2012

(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 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 = “”)
#   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:

# 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,
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 their blog: R snippets. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)