[R] Points but no lines in qplot.

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Jun 30 16:21:53 CEST 2011


This is probably due to a numerical value that is coded as a factor. Have a look at the example below.
In ggplot2 one line per group is plotted. The default grouping is the combination of all factors. In the first example only B, in the second and third example both A and B. This leaves just one observation per group and hence the line cannot be displayed.

library(ggplot2)
dataset <- expand.grid(A = 1:10, B = factor(LETTERS[1:3]))
dataset$Y <- rnorm(nrow(dataset), mean = dataset$A, sd = as.numeric(dataset$B))
#this is the plot that you want
ggplot(dataset, aes(x = A, y = Y, colour = B)) + geom_line() + geom_point()
#when A is a factor, then only the points are displayed
ggplot(dataset, aes(x = factor(A), y = Y, colour = B)) + geom_line() + geom_point()
#setting the grouping correctly, solves the problem.
ggplot(dataset, aes(x = factor(A), y = Y, colour = B)) + geom_line(aes(group = B)) + geom_point()

Best regards,

Thierry

PS Note that there is also a ggplot2 specific mailing list.

> -----Oorspronkelijk bericht-----
> Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
> Namens Ashim Kapoor
> Verzonden: donderdag 30 juni 2011 16:09
> Aan: Sarah Goslee
> CC: r-help op r-project.org
> Onderwerp: Re: [R] Points but no lines in qplot.
> 
> Two things I'd try:
> 
> > run your code in a clean R session, started at the command line with R
> > --vanilla and just loading that exact dataset and running that line of
> > code, in case there are issues with other things in your session.
> >
> > Upgrade to the current version of R.
> >
> >
> Dear Sarah,
> 
> I upgraded to the new R and tried R --vanilla but I still see no lines. In the
> legend,as earlier,I see a line through a point but in the graph I see only points.
> No lines. Any ideas now ?
> 
> > sessionInfo()
> R version 2.13.0 (2011-04-13)
> Platform: i486-pc-linux-gnu (32-bit)
> 
> locale,
>  [1] LC_CTYPE=en_IN       LC_NUMERIC=C         LC_TIME=en_IN
>  [4] LC_COLLATE=en_IN     LC_MONETARY=C        LC_MESSAGES=en_IN
>  [7] LC_PAPER=en_IN       LC_NAME=C            LC_ADDRESS=C
> [10] LC_TELEPHONE=C       LC_MEASUREMENT=en_IN LC_IDENTIFICATION=C
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help op r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list