-1

So i have this data, that I would like to plot onto a graph - all the lines on the same graph

>ndiveristy
 Quadrant nta.shannon ntb.shannon ntc.shannon
1        1    2.188984   0.9767274   1.8206140
2        2    1.206955   1.3240481   1.3007058
3        3    1.511083   0.5805081   0.7747041
4        4    1.282976   1.4222243   0.4843907
5        5    1.943930   1.7337267   1.5736545
6        6    2.030524   1.8604619   1.6860711
7        7    2.043356   1.5707110   1.5957869
8        8    1.421275   1.4363365   1.5456799

here is the code that I am using to try to plot it:

ggplot(ndiversity,aes(x=Quadrant,y=Diversity,colour=Transect))+
       geom_point()+
       geom_line(aes(y=nta.shannon),colour="red")+
       geom_line(aes(y=ntb.shannon),colour="blue")+
       geom_line(aes(y=ntc.shannon),colour="green")

But all I am getting is the error

data must be a data frame, or other object coercible by fortify(), not a numeric vector.

Can someone tell me what I'm doing wrong

6
  • ndiversity apparently is not a data frame. Trying running class(ndiveristy) Commented Sep 2, 2022 at 10:10
  • Where are the variables Diversity and Transcect form? They seem not to be present in your data? Also, could you post your data with dput()? Commented Sep 2, 2022 at 10:12
  • @VvdL class(ndiversity) [1] "matrix" "array" Commented Sep 2, 2022 at 10:13
  • right, you will have to create a data frame from that matrix before using it with ggplot. Commented Sep 2, 2022 at 10:15
  • 1
    I did have a line of code to convert it to being a data frame, but I hadn't inputted correctly. We got there tho I'm still much better at the biology side than the coding side but thanks for your help folks (y) Commented Sep 2, 2022 at 10:22

2 Answers 2

1

Typically, rather than using multiple geom_line calls, we would only have a single call, by pivoting your data into long format. This would create a data frame of three columns: one for Quadrant, one containing labels nta.shannon, ntb.shannon and ntc.shannon, and a column for the actual values. This allows a single geom_line call, with the label column mapped to the color aesthetic, which automatically creates an instructive legend for your plot too.

library(tidyverse)

as.data.frame(ndiversity) %>%
  pivot_longer(-1, names_to = 'Type', values_to = 'Shannon') %>%
  mutate(Type = substr(Type, 1, 3)) %>%
  ggplot(aes(Quadrant, Shannon, color = Type)) + 
  geom_line(size = 1.5) +
  theme_minimal(base_size = 16) +
  scale_color_brewer(palette = 'Set1')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

For posterity:

  1. convert to data frame
ndiversity <- as.data.frame(ndiversity)
  1. get rid of the excess code
ggplot(ndiversity,aes(x=Quadrant))+
                     geom_line(aes(y=nta.shannon),colour="red")+
                     geom_line(aes(y=ntb.shannon),colour="blue")+
                     geom_line(aes(y=ntc.shannon),colour="green")
  1. profit

not the prettiest graph I ever made

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.