Stackuser
Stackuser

Reputation: 59

Graphing using R

I want to draw multiplot graphing using and I want the value of in X-axis for each site and the different lines in the graph are M, T, L, and G but at the beginning of the code, I got an error,

$ operator is invalid for atomic vectors

Graphdata <- as.data.frame(multiplot)
par(mfrow=c(4,3))
plot(Graphdata$Sites$A, Graphdata$f, ylim=c(0,16), xlab="Number of years", 
     ylab="Relative density", lwd=2)
lines(Graphdata$Sites$M, type="l", col="blue", lwd=2)
lines(Graphdata$Sites$T, type="l", col="green", lwd=2)
lines(Graphdata$Sites$L, type="l", col="grey", lwd=2)
lines(Graphdata$Sites$G, type="l", col="orange", lwd=2)

and some of my data is

Upvotes: 1

Views: 82

Answers (2)

Curtis
Curtis

Reputation: 459

You could tack on to your ggplot call using the facet_wrap function and use the 'MySite' variable as your faceting variable

Upvotes: 0

NelsonGon
NelsonGon

Reputation: 13319

You need to subset as follows:

SiteA<-Graphdata[Graphdata$Sites=="A",]

plot(SiteA$f,SiteA$M)

par(mfrow=c(1,1))

That said, I'm a fan of the tidyverse and think it would give you an easier solution. You'll need to reshape2 your data although gather can do just fine. EDIT You'll also need to decide what to do with the missing values.

library(tidyverse)
Graphdata %>% 
  gather("MySite","MyValue",3:ncol(.)) %>% 
  filter(Sites=="A") %>% 
  ggplot(aes(`f`,MyValue,col=MySite))+geom_point()+geom_line()

This yields: enter image description here

Upvotes: 1

Related Questions