r - How can I force 'scatter.smooth' to label the x-axis with strings rather than numbers, ordered the way I mean it? -
i experimenting r's scatter.plot , have written following scriptlet:
d <- data.frame( weekday = rep(c('so', 'mo', 'tu', 'we', 'th', 'fr', 'sa'), 5), value = nchar(c( '##' , # '########' , # mo '#############' , # tu '######' , # '############' , # th '###############' , # fr '###' , # sa # --- '####' , # '####' , # mo '########' , # tu '#####' , # '###########' , # th '#########' , # fr '#####' , # sa # --- '####' , # '######' , # mo '############' , # tu '####' , # '#############' , # th '##########' , # fr '###' , # sa # --- '#' , # '####' , # mo '##############' , # tu '####' , # '############' , # th '##############' , # fr '#######' , # sa # --- '###' , # '######' , # mo '###########' , # tu '######' , # '#############' , # th '###########' , # fr '####' # sa )) ) scatter.smooth(d$value ~ d$weekday, col=gray(0.7), bty='n') this produces following plot: 
there 2 problems me: x-axis labeled numbers rather so, mo through sa , numbers seems ordered according alphabetical value of weekday represent (so 1 fr , 7 we).
how can have plot show me weekday labels (so through sa) in order of weekdays?
first you'll want reorder factor levels of weekday (which correctly noticed defaults alphabetical):
d$weekday = factor(d$weekday, levels= c('so', 'mo', 'tu', 'we', 'th', 'fr', 'sa')) then you'll want use levels of weekday data replace x-axis's numbers:
scatter.smooth(d$value ~ d$weekday, col=gray(0.7), bty='n', xaxt="n") axis(1, at=1:7, labels = levels(d$weekday)) adding xaxt="n" says first not use default number scheme x-axis, axis function places custom labels.

Comments
Post a Comment