0

I am trying to plot a graph in which the x-axis has non-numeric data represented by labels created from numeric data. Let me explain with my mock example. The data.log file used in my example can be found in here [pastie.org].

When executing my code:

library('Hmisc')
# Please find the "data.log" file in http://pastie.org/7943338
data = read.table("data.log", col.names=c('DAY','PERIOD','UID','NUMBER'), colClasses=c("UID"="character"))
attach(data)
a = summarize(NUMBER, llist(PERIOD, UID), smedian.hilow, na.rm=T)
detach(data)
xYplot(Cbind(NUMBER, Lower, Upper)~numericScale(UID)|factor(PERIOD), data = a)

Returns me this graphic:

Graphic with numeric x-axis

What I really wanted is to plot the x values as characters, as if UID was a string label, but not as its numeric value because they are not sequential and, therefore, they are not equally distributed on the x-axis.

I've already struggled with many things to accomplish that, I've seen examples regarding factors (1, 2, 3) non-numeric axis (1, 2), but I was not successful on looking for something similar to that. Not exclusively looking for someone to solve my problem, therefore, links for examples, docs, or techniques to help me find the solution are more than welcome. Thanks.

Eduardo
  • 4,282
  • 2
  • 49
  • 63
  • 1
    Can you create a quick sketch or mock-up of what you want? That would improve the question significantly. Also, are you open to moving away from the 'xYplot' function? The 'ggplot2' or 'grid' packages are much more flexible and may be less effort to learn and implement than hacking the 'xYplot' into the shape you want. – Dinre May 22 '13 at 14:37
  • @Dinre, yes, I am going to create the sketch. And yes, I am open to use anything as long as I am able to accomplish my needs. Thanks for suggesting me ggplot2 and grid. – Eduardo May 22 '13 at 14:42

1 Answers1

2

Here a solution using ggplot2 and geom_pointrange:

library(ggplot2)
ggplot(data=a, aes(x     = factor(UID),
                   y     = NUMBER,
                   ymin  = Lower,
                   ymax  = Upper  )) +
  geom_pointrange() +
  facet_grid(PERIOD~.)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Oh! That is amazing! Dinre suggested me ggplot2 and I was reading docs. Your solution is beautiful and really what I wanted! Thanks! – Eduardo May 22 '13 at 15:17