Jeremy
Jeremy

Reputation: 393

ggplot does not plot data in the given order

I'm trying to plot some date points:

library(ggplot2)
library(plyr)
set.seed(1234)

df <- data.frame(
  c1 = as.character(1:10),
  c2 = sample(seq(from=as.Date("2012-01-01"),to=as.Date("2012-12-01"),by="day"), 10)
)
df <- arrange(df,c2)
qplot(c2,c1,data=df) + scale_x_date(breaks="1 month")

Although I used arrange to sort the data, ggplot does not plot the data in order: enter image description here

The expected plot is:

enter image description here

How do I tell ggplot to plot the c1 column in the given order rather than the natural sort order for the "character" type ?

SessionInfo():

R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
 [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8    LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plyr_1.7.1      ggplot2_0.9.2.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-0   dichromat_1.2-4    digest_0.5.2       grid_2.14.1        gtable_0.1.1       labeling_0.1      
 [7] MASS_7.3-16        memoise_0.1        munsell_0.4        proto_0.3-9.2      RColorBrewer_1.0-5 reshape2_1.2.1    
[13] scales_0.2.2       stringr_0.6.1      tools_2.14.1      

dput(df):

structure(list(c1 = structure(c(8L, 1L, 9L, 2L, 4L, 5L, 3L, 7L, 
10L, 6L), .Label = c("1", "10", "2", "3", "4", "5", "6", "7", 
"8", "9"), class = "factor"), c2 = structure(c(15343, 15378, 
15416, 15508, 15543, 15547, 15548, 15551, 15558, 15625), class = "Date")), .Names = c("c1", 
"c2"), row.names = c(NA, -10L), class = "data.frame")

Upvotes: 1

Views: 3124

Answers (1)

IRTFM
IRTFM

Reputation: 263342

There is no problem (with R).

You have been plotting a random permutation of dates against a character vector of digits that were coerced from integer. You are getting exactly what you asked for. No need to reinstall R or any packages. The dates in the 'c2' column will get sorted in the process of constructing the plot, so there is no need to pre-sort them and doings so will have no effect.

If on the the hand you want the the labels on the y axis to be the c1-values but the y-values to be the order-statistic (the integer order), then you need to plot 1:length(c1) against the date and change the default labels. (This remains a difficulty in your understanding of the expected behavior of a plotting process. There was no need to reinstall R.) This is the base graphics solution:

> with(df, plot(c2,1:10, yaxt="n"))
> axis(2, at=1:10, labels=df$c1)

I find the ggplot documentation rather frustrating. Try to find how to substitute axis labels is something that is just not apparent. I tried update_labels without success. The use of a different language for plotting entities in that system remains an ongoing barrier for me. I know that many people have the opposite reaction and that ggplot2 is a favorite package for many. (Edit: this appears to create the desired plot in the ggplot world:)

> p <- qplot(c2, 1:nrow(df),data=df) + scale_x_date(breaks="1 month")
> p
> p + scale_y_discrete( labels=df$c1)

Upvotes: 3

Related Questions