1

Struggled with this question for a while and not sure what's going on. I know this question will definitely demonstrate my deficiency with ggplot.

I have a script like this that functions nicely:

beta.bray= c(0.681963714,0.73301985,0.6797153,0.79358052,0.85055556,0.76297686,0.60653007)

beta.bray.gradient=c(0.182243513, 0.565267411,0.427449441,0.655012391,0.357146893,0.286457524,0.338706138)

Date=c("07/18/14","07/26/14","08/19/14","08/25/14", "07/25/15","08/22/15", "07/26/16")
dat=data.frame(Date, beta.bray, beta.bray.gradient)

test<-ggplot(dat, aes(x=reorder(Date, x=fct_inorder(Date)), y=beta.bray, group=1))+geom_line(linetype="dashed")+geom_point()+
  labs(x="Date", y="β, multiple-site dissimilarity", title="SNARL riffle site/site β through time, 2014-2016") +coord_cartesian(xlim=c(1,7),ylim=c(.58,.85))

test

But when I want to add another line for beta.bray.gradient, I can't get anything to work. I think it has something to do with the way I used aes() in the above code, but I didn't know how else to do it, in order to use reorder() and fct_inorder() to make sure the dates are plotted in the right way. Here's an example of a way I tried adding the second line:

    test<-ggplot(dat, aes(x=reorder(Date, x=fct_inorder(Date)), y=beta.bray, group=1))+geom_line(linetype="dashed")+geom_point()+
  geom_line(dat, aes(y=beta.bray.gradient, linetype="c"))+
  labs(x="Date", y="β, multiple-site dissimilarity", title="SNARL riffle site/site β through time, 2014-2016") +coord_cartesian(xlim=c(1,7),ylim=c(.58,.85))

In these situations we see a multitude of errors, in this case Error: ggplot2 doesn't know how to deal with data of class uneval

1 Answer 1

1

I would think it best to use actual date objects for the x axis and reshape your data into a long format:

library(dplyr)
library(tidyr)
library(ggplot2)

beta.bray <- c(0.681963714,0.73301985,0.6797153,0.79358052,0.85055556,0.76297686,0.60653007)
beta.bray.gradient <- c(0.182243513, 0.565267411,0.427449441,0.655012391,0.357146893,0.286457524,0.338706138)
Date <- as.Date(c("07/18/14","07/26/14","08/19/14","08/25/14", "07/25/15","08/22/15", "07/26/16"),"%m/%d/%y")

dat <- data.frame(Date, beta.bray, beta.bray.gradient) %>%
    gather(key = "grp",value = "val",beta.bray,beta.bray.gradient)

ggplot(dat, aes(x = Date, y = val, group = grp,color = grp)) + 
    geom_line(linetype="dashed") + 
    geom_point() + 
    labs(x="Date", y="β, multiple-site dissimilarity", 
             title="SNARL riffle site/site β through time, 2014-2016") + 
    coord_cartesian(xlim=Date[c(1,7)])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.