[R] Odp: Plot of a subset of a data.frame()

Petr PIKAL petr.pikal at precheza.cz
Mon Jul 26 14:03:33 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 26.07.2010 13:38:44:

> Hello,
> 
> my data.frame is sort of a collection of process values, i.e. huge 
> run-chart. It consists of a time-stamp in the first column (date as 
> string), factors in the following columns (used for subset-filtering), 
> and some process-data columns.
> Hereafter, two examples are listed, showing the problems that occour 
> during print:
> 
> At first the example, that works fine:
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> a = c(1:10)       # create a vector of integers
> b = rep(c("a","b"),5)   # create a vector of chars, used
>          # as factor-levels 
> d = rnorm(10)      # some random numbers
> e = data.frame(a,b,d)   # connect to a data.frame
> 
> e.1 = subset(e, b=="a")   # create two subsets
> e.2 = subset(e, b=="b")
> plot(d~a, e.1, pch=3, col=2) # plot first data-subset

Rather strange plot call. I usually call

plot(a, d, pch=as.numeric(as.factor(b))+2, col=as.numeric(as.factor(b))+1)

as you could have problem when some point in second subset is outside a 
range of first subset.

> points(d~a, e.2, pch=4, col=3) # plot the 2nd one
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> all looks fine in theses plots.
> 
> 
> However, changing the content of vector "a" to a set of strings the 
> following happens:
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> a = c("a","b","c","d","e","f","g","h","i","j")
> e = data.frame(a,b,d)       # re-build data.frame
> 
> e.1 = subset(e, b=="a")     # create two subsets
> e.2 = subset(e, b=="b")
> plot(d~a, e.1, pch=3, col=2)
> points(d~a, e.2, pch=4, col=3)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> The plot-command produces horizontal lines instead of dots. This seems 
> to happen when the x-axis contains strings rather than numbers. is there 

> a way out?

You actually called boxplots hence lines and labels under x axis. The way 
out depends on how do you want everything to be plotted. If "a vector" was 
a factor you could use conversion to numeric representation by

a.n<-as.numeric(a)

and plot d against a.n with axis labels from a.

Try to go through
plot, plot.default, boxplot, factor

help pages

Regards
Petr

> 
> Best regards,
> /Steffen
> -- 
> Steffen Uhlig, PhD
> Mechatronik und Sensortechnik
> HTW des Saarlandes
> Goebenstraße 40
> 66117 Saarbrücken
> 
> Tel.: +49 (0) 681 58 67 274
> 
> ______________________________________________
> R-help at 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