[R] Subsetting data leads to funky plots

Sarah Goslee sarah.goslee at gmail.com
Sat Nov 12 15:04:06 CET 2011


Hi,

On Sat, Nov 12, 2011 at 2:04 AM, Vinny Moriarty <vwmoriarty at gmail.com> wrote:
> I'm trying out a basic plot, but something about the way I subset my data
> leads to problems with the plot.
>
>
> Here is the first bit of my data set
>
> year,date,location,quadrat_juvenile,photo_location,photo_exists,genus,count,divers
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Acanthastrea,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Acropora,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Astreopora,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Cyphastrea,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Favia,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Fungia,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Gardinoseris,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Herpolitha,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Leptastrea,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Leptoseris,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Lobophyllia,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Millepora,0,HP+MEM
> 2005,2005-04-30, 1 Fringing Reef,1, 1 Fringing Reef Coral Transect Pole 1-2
> Quadrat 1,t,Montastrea,0,HP+MEM

dput() with a sample of your data would be useful, so that we ca try
it. Among other things, the example you give above doesn't *have* the
values that you're subsetting on, so we can't easily try your specific
code.

str() of your data would also be useful. Are these factors? Strings?

The small reproducible example is important.


> I need to breakdown the data before I can plot it, so this is the stepwise
> code I use:
>
> Juv=read.csv("juvenile_density_20110506.csv")
> Juv$count[Juv$count<0]<- NA #Missing data is imputed as negative data, so
> first label it as NA
> JuvFor=subset(Juv,(location==" 2 Outer 10 m") | (location==" 1 Outer 10
> m")) #Subset the data of interest
> Juv_Sum_by_quad=aggregate(count~year+location+quadrat_juvenile,data=
> JuvFor,sum) # Calculate sum of each quadrat
> Juv_Avg=aggregate(count~year+location,data=Juv_Sum_by_quad, mean)
> #Calculate yearly means
>
> So far so good.....

Is it? Did you look at Juv_Avg to make sure that it contains what you expect?

>
> I thought I could do this:
>
> plot(Juv_Sum1$year, Juv_Sum1$count,type="L")


There's no type="L", although there is type="l"

Is Juv_Sum1 sorted by year? Without a reproducible example, I can't
see which of the many things that could go wrong have gone wrong. R
does exactly what you tell it, and it sounds like you may have given
it data that weren't sorted.

For that matter, there's no Juv_Sum1 in the code you gave, so I don't
have the slightest idea what might be in it.

> But not only do I get odd separate lines as opposed to a time series line
> plot, but I the first tick mark on the x-axis lists the amount of rows in
> my data. I can fix it by saving the final object as a .csv and reloading it
> the data- but that defeats the whole purpose. Can anyone explain why I get
> this plot result?
>
> Looking through past posts I found that I can get away with just plotting
> the y-axis. And if I modify the x axis separately I can get the plot I'm
> looking for.
>
> plot(Juv_Sum1$count,xaxt="n")
> axis(1,at=1:length(Juv_Sum1$year),labels=Juv_Sum1$year)
>
>
> But I'm concerned about why my data subsets are giving me odd results in
> the first place, especially as I move on into more complicated bits of code.

This list is generally quite helpful, but not telepathic. See these
two lines from every message (below)? That's what we need.

> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Sarah
-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list