Creating multiple sets of labels for the x and y axes

801 views
Skip to first unread message

Brian

unread,
Feb 4, 2011, 12:16:18 PM2/4/11
to ggp...@googlegroups.com
I wanted to resurrect this thread in light of some design considerations I'm going through.

Regarding multiple axes, Hadley wrote last year:
Laying the ground work for a secondary transformed axis is on my to do list for this summer. 
I was wondering whether it's possible to have multiple sets of labels for the same breaks. In Andrie's code, he solves the problem of having both fahrenheit and celsius values by concatenating them. He passes farhenheit-celsius pairs as entries to the "labels" argument of scale_x_continuous:

## Andrie's Code
degC <- seq(0,100,10) 
degCandF <- paste(degC,"C / ",degC * 9 / 5 + 32,"F",sep="") 
d <- data.frame(tempcelcius=round(rnorm(25,15, 10), 1),attendence=rnorm(25,500, 100))
p <- qplot(x=tempcelcius,y=attendence,data=d) 
p <- p + scale_x_continuous("Temperature (Celsius/Fahrenheit)",breaks=degC,labels=degCandF) 
print(p)

In the code below, I create a situation—albeit a slightly nonsense one—where for international audiences it might be sensible to have converted values appear.

## My Code
set.seed(seed = 1001)
Dollars      <- seq(20000, 40000, 2000) 
DollarLabels <- paste("$", Dollars)

Pounds       <- seq(6, 11, 1)
PoundLabels  <- paste(Pounds, "lbs.")

d <- data.frame(AnnualIncome = rnorm(n = 25, mean = 30000, sd = 2500), BirthWeight = rnorm(n = 25, mean = 8, sd = 1)) 
p <- qplot(x = AnnualIncome, y = BirthWeight, data = d) 
p <- p + scale_x_continuous("Annual Income", breaks = Dollars, labels = DollarLabels)
p <- p + scale_y_continuous("Birth Weight", breaks = Pounds, labels = PoundLabels) 
print(p) 

For example, the graph might be useful to international audiences if they could view it in terms of other currencies and international standard weights. In the image I've created (attached), I made up the currency and weight conversions for ease of mock-up. The key feature, though, is to assume that the major gridlines represent the same real unit of weight/money, and all I'm doing is supplying labels that respectively represent that weight/amount through a localised conversion. I don't want new breaks, just the ability to supply multiple sets of converted labels—in different positions—for the same underlying break numbers.

Does that make sense? If so, my colleague and I were wondering whether the solution might lie in either a crafty geom_text() call, or creating a modified geom based off of geom_text() to handle placing the labels. (We're not sure how we'd implement it exactly, yet.)

Alternatively, if you think there are better ways to display converted information for the same breaks, I'd love to hear your suggestions. It just seemed like having converted values on another axis was sensible. But I welcome your feedback and input!

- Brian

Gggplot2MultipleAxisLabels-1.jpg

tjulou

unread,
Mar 25, 2011, 9:53:18 AM3/25/11
to ggplot2
Hi,

I have no clue on how to implement it precisely… But for sure, I would
use it as well!
I want to emphasize that this is not only only useful for unit
conversion, but when two physical variables are linked by a linear
relationship (for instance space and temperature in the case of a
linear temperature gradient)…

Any progress on this?

Best,

Thomas



On Feb 4, 6:16 pm, Brian <brian.danie...@gmail.com> wrote:
> I wanted to resurrect this thread<https://groups.google.com/d/topic/ggplot2/YVVEgqQGRXI/discussion>in light of some design considerations I'm going through.
>  Gggplot2MultipleAxisLabels-1.jpg
> 71KViewDownload

Brian Danielak

unread,
Nov 10, 2011, 7:36:43 PM11/10/11
to ggp...@googlegroups.com
I've found a rather kludgy solution; one which I've implemented in my package granovaGG.

Basically, we create a set of transformed x-axis labels by:
  1. Drawing a huge white rectangle on top of the graph (to match the background)
  2. Drawing our own group label names using geom_text()
  3. Drawing our own group size indicators using geom_text()
In the example below, we use the mpg dataset to show highway mileage as a function of manufacturer. On the bottom x-axis, you see numerical indicators of the main effect of each manufacturer. On the top, you can see the corresponding manufacturer labels, along with numbers indicating how many cars from each manufacturer (group sizes)

install.packages("granovaGG", dependencies = TRUE) # Install our package if you don't have it already
library(granovaGG)
with(mpg, granovagg.1w(hwy, group = manufacturer)) # generate sample graphic


Hadley Wickham

unread,
Nov 13, 2011, 7:07:54 PM11/13/11
to ggp...@googlegroups.com
This should be a bit easier in the next version since you'll be able to replace the axis grob with a grob of your choice.

Hadley


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages