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

Alain Guillet alain.guillet at uclouvain.be
Mon Jul 26 13:56:00 CEST 2010


  Hello,

It is completely normal. I advise you to read the manual "An 
introduction to R" on the CRAN website. For example you can find (part 
12.1.1) :


        12.1.1 The |plot()| function

  One of the most frequently used plotting functions in R is the 
|plot()| function. This is a /generic/ function: the type of plot 
produced is dependent on the type or /class/ of the first argument.

|plot(|x|, |y|)|
|plot(|xy|)|
    If x and y are vectors, |plot(|x|, |y|)| produces a scatterplot of y
    against x. The same effect can be produced by supplying one argument
    (second form) as either a list containing two elements x and y or a
    two-column matrix.
|plot(|x|)|
    If x is a time series, this produces a time-series plot. If x is a
    numeric vector, it produces a plot of the values in the vector
    against their index in the vector. If x is a complex vector, it
    produces a plot of imaginary versus real parts of the vector elements.
|plot(|f|)|
|plot(|f|, |y|)|
    f is a factor object, y is a numeric vector. The first form
    generates a bar plot of f; the second form produces boxplots of y
    for each level of f.
|plot(|df|)|
|plot(~ |expr|)|
|plot(|y| ~ |expr|)|
    df is a data frame, y is any object, expr is a list of object names
    separated by `|+|' (e.g., |a + b + c|). The first two forms produce
    distributional plots of the variables in a data frame (first form)
    or of a number of named objects (second form). The third form plots
    y against every object named in expr. 



Alain


On 26-Jul-10 13:38, Steffen Uhlig wrote:
> 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
> 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?
>
> Best regards,
> /Steffen

-- 
Alain Guillet
Statistician and Computer Scientist

SMCS - IMMAQ - Université catholique de Louvain
Bureau c.316
Voie du Roman Pays, 20
B-1348 Louvain-la-Neuve
Belgium

tel: +32 10 47 30 50



More information about the R-help mailing list