Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot with Strings on x-Axis

I'm trying to draw a plot with several curves in it. The x-axis are not numerical values, but Strings.

This works fine (like in how to plot all the columns of a data frame in R):

require(ggplot2)
df_ok <- rbind(data.frame(x=4:1,y=rnorm(4),d="d1"),data.frame(x=3:1,y=rnorm(3),d="d2"))
ggplot(df_ok, aes(x,y)) + geom_line(aes(colour=d))

But my data looks like this:

require(ggplot2)
df_nok <- rbind(data.frame(x=c("four","three","two","one"),y=rnorm(4),d="d1"),data.frame(x=c("three","two","one"),y=rnorm(3),d="d2"))
ggplot(df_nok, aes(x,y)) + geom_line(aes(colour=d))

I get the error geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?. Even though the graph lines do not appear, the axis are plotted, and the x-Axis contains the correct Labels - but also in wrong order.

Any idea how to plot this as easy as possible? (Also note the missing x-values for some series).

like image 375
fabb Avatar asked May 08 '12 09:05

fabb


2 Answers

Your problem is that the x variable is a factor. So, change your data frame and make x a double:

df = rbind(data.frame(x=4:1,y=rnorm(4),d="d1"), 
           data.frame(x=3:1,y=rnorm(3),d="d2"))

Plot as normal

g = ggplot(df, aes(x,y)) + geom_line(aes(colour=d))

but alter the x-axis scaling explicitly:

g + scale_x_continuous(breaks=1:4, labels=c("one", "two", "three", "four")) 

To rename your variable, try something like:

x1 = factor(df_nok$x, 
            levels=c("one", "two", "three", "four"), 
            labels=1:4)
df$x1 = as.numeric(x1)
like image 198
csgillespie Avatar answered Sep 21 '22 12:09

csgillespie


You can convince ggplot to draw lines by adding a dummy group,

ggplot(df_nok, aes(x,y)) + geom_line(aes(colour=d, group=d))

Also see http://kohske.wordpress.com/2010/12/27/faq-geom_line-doesnt-draw-lines/

like image 23
baptiste Avatar answered Sep 21 '22 12:09

baptiste