rgraphplotsubsetmissing-data

Why does my points() code not work? Plotting two variables with subsets of a third in r


Firstly, my dataset looks like this: (an example)

 Sample_type   Chick   Adult
 Down           11     -23
 Down           10     -25
 Feather               -22
 Feather         9     -24
 Primary        10.5   -22.5
 No adult       11.5

I want to make a scatter graph with two variables (Chick and Adult), that is 'separated' as it were by subsets of a third variable (Sample_type). I was taught to do it in a university course using the code:

plot(Chick~Adult,type="n") points(Chick~Adult,subset=(Sample_type="Down"),col="blue") points(Chick~Adult,subset=(Sample_type="Feather"),col="red") points(Chick~Adult,subset=(Sample_type="Primary"),col="green")

However when I do this, no points appear on the chart (ie, the first line of code 'works' but the next three don't). I receive no code errors or warnings.

I've looked around for similar questions and the closest I could find was this: How to plot a subset of a data frame in R? But after trying with(chick_v_adult[chick_v_adult$Sample_type="Down",], plot(Chick,Adult)) and plot(Chick[Sample_type="Down"],Adult[Sample_type="Down"]) according to the code suggested, I received the errors

Error: unexpected '=' in "with(chick_v_adult[chick_v_adult$Sample_type="

and

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf

respectively. Which then lead me to this: Error in plot.window(...) : need finite 'xlim' values - I wasn't sure how to apply the suggestions here to my specific problem.

So - I'm pretty sure it's something to do with either the missing data or the nature of the third variable I'm trying to subset. I don't know a) how to find out if it IS a problem like that with the data, or if it's a problem with my code (although I don't think the code is the problem), or b) what adjustments I should actually make to my data and/or code, to get the graph I want!

Help or directions to help elsewhere much appreciated.

(NB: I've also come across this: R plotting a dataset with NA Values but I don't want to do this as my data isn't linear, so any fill-ins would be completely random and incorrect. Also, the missing data isn't a problem when I simply do plot(Chick~Adult) - the lines with missing data are automatically not included in the resulting graph.)


Solution

  • I figured out the code I needed to use to get them onto the same graph - thanks Danny for the help though.

    The code that worked is:

    #create an empty plot
    plot(Chick~Adult,type="n")
    
    #create separate vectors for each subset you want
    Dvalues<-chick_v_adult[chick_v_adult$Sample_type=="Down",]
    Fvalues<-chick_v_adult[chick_v_adult$Sample_type=="Feather",]
    Pvalues<-chick_v_adult[chick_v_adult$Sample_type=="Primary",]
    
    #plot the points using the new vectors
    points(Dvalues$Chick~Dvalues$Adult,col="Red",pch=16)
    points(Fvalues$Chick~Fvalues$Adult,col="Blue",pch=16)
    points(Pvalues$Chick~Pvalues$Adult,col="Green",pch=16)
    
    #plot lines over the points
    abline(lm(Dvalues$Chick~Dvalues$Adult),col="Red")
    abline(lm(Fvalues$Chick~Fvalues$Adult),col="Blue")
    abline(lm(Pvalues$Chick~Pvalues$Adult),col="Green")