Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a hierarchical structure on the axis labels with ggplot2?

Tags:

r

ggplot2

I would like to create a plot like this with ggplot2: Barplot with hierarchical labels on the x axis

(I made a small error here that caused not all labels to show up. obviously I do want all labels to show up :) ) However, my Google search didn't turn up anything. The closest I got was with barNest from plotrix, however, I don't like the quality of the graphs that are produced. The ggplot2 ones look a lot better.

In the ideal case, I would have expected to be able to do something like this: scale_x_discrete("Axis Title", labels=Level1+Level2+Level3) And then Level1, Level2 and Level3 would have been columns of the data frame that was used in the qplot function. If I can get this to work, I want to create a function to add this type of functionality to the scales function. Maybe scale_x_hierarchical would be better. Similar data can be generated with the following code:

data <- data.frame(
  Subject = c(rep(1, 48), rep(2, 48), rep(3, 48), rep(4, 48)), 
  month = rep(c(rep(1, 12), rep(4, 12), rep(7, 12), rep(10, 12)), 4), 
  day = rep(c(rep(1, 4), rep(11, 4), rep(21, 4)), 16), 
  hour = rep(c(0, 6, 12, 18), 48), 
  data = rnorm(192))

By the way, the image used here is created with Spotfire.

like image 934
wligtenberg Avatar asked Apr 18 '11 14:04

wligtenberg


1 Answers

It's a little different, but the LearnR blog has a post on linking a table to a graph so that it looks like this:

enter image description here

like image 97
djq Avatar answered Oct 14 '22 18:10

djq