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.

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top