0

I'm pretty new to R so I don't really know what I'm doing. Anyway, I have data in this format in excel (as a csv file):

dt <- data.frame(species = rep(c("a", "b", "c"), each = 4),
                 cover = rep(1:3, times = 4),
                 depth = rep(c(15, 30, 60, 90), times = 3),
                 stringsAsFactors = FALSE)

I want to plot a graph of cover against depth, with a different coloured line for each species, and a key for which species is which colour. I don't even know where to start.

Sorry if something similar has been asked before. Any help would be much appreciated!

Don't know if this is in a helpful format but here's some of the actual data, I need to read more about dput I think:

structure(list(species = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 
11L), .Label = c("Agaricia fragilis", "bryozoan", "Dichocoenia stokesi", 
"Diploria labyrinthiformis", "Diploria strigosa", "Madracis decactis", 
"Manicina", "Montastrea cavernosa", "Orbicella franksi", "Porites asteroides", 
"Siderastrea radians"), class = "factor"), cover = c(0.021212121, 
0.04047619, 0, 0, 0, 0, 1.266666667, 4.269047619, 3.587878788, 
3.25, 0.118181818, 0.152380952, 0, 0.007142857, 3.806060606, 
2.983333333, 14.13030303, 15.76190476, 0.415151515, 0.2, 0.26969697, 
0.135714286), depth = c(30L, 15L, 30L, 15L, 30L, 15L, 30L, 15L, 
30L, 15L, 30L, 15L, 30L, 15L, 30L, 15L, 30L, 15L, 30L, 15L, 30L, 
15L)), .Names = c("species", "cover", "depth"), row.names = c(NA, 
22L), class = "data.frame")
www
  • 38,575
  • 12
  • 48
  • 84
user8144422
  • 101
  • 1
  • 8
  • It would be great if you can post an example of your dataset, not an image of the screenshot, which will make helps much easier. – www Jun 11 '17 at 20:39
  • Not entirely sure wht you mean, heres what some of it looks like: species cover depth 1 Agaricia fragilis 0.021212121 30 2 Agaricia fragilis 0.040476190 15 3 bryozoan 0.000000000 30 4 bryozoan 0.000000000 15 5 Dichocoenia stokesi 0.000000000 30 6 Dichocoenia stokesi 0.000000000 15 7 Diploria labyrinthiformis 1.266666667 30 8 Diploria labyrinthiformis 4.269047619 15 9 Diploria strigosa 3.587878788 30 10 Diploria strigosa 3.250000000 15 – user8144422 Jun 11 '17 at 21:02
  • Please do not post the updated information by comments. Updated the original post and use the `dput` function. – www Jun 11 '17 at 21:03
  • See this post to learn more about how to post a reproducible example (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), – www Jun 11 '17 at 21:05
  • I have posted an answer based on your original example dataset. Notice that I used R code to create the example data frame based on the information from your image. If you can use this kind of code to make your example dataset reproducible, it would be easier for others to help. Image of screenshot is bad to share data. – www Jun 11 '17 at 21:20

2 Answers2

2

Here is a solution using the ggplot2 package.

# Load packages
library(ggplot2)

# Create example data frame based on the original example the OP provided
dt <- data.frame(species = rep(c("a", "b", "c"), each = 4),
                 cover = rep(1:3, times = 4),
                 depth = rep(c(15, 30, 60, 90), times = 3),
                 stringsAsFactors = FALSE)

# Plot the data
ggplot(dt, aes(x = depth, y = cover, group = species, colour = species)) +
  geom_line()
www
  • 38,575
  • 12
  • 48
  • 84
0

This should get you going!

  df1 <- read.csv("//file_location.csv", headers=T)
  library(dplyr)
  df1 <- df1 %>% select(species, depth) %>% group_by(species) %>%
  summarise(mean(depth)
  library(ggplot2)
  ggplot(df1, aes(x=depth, y=species, group=species, color=species) +
  geom_line()
zdeeb
  • 142
  • 9
  • Thanks. Though R is prompting me for something else after the third line. – user8144422 Jun 11 '17 at 20:33
  • The OP mentioned about plotting a graph of cover against depth. Although it is not completely clear to me what plots the OP wants, your `ggplot2` did not include any information about `cover`, so I guessed this answer may not be valid. – www Jun 11 '17 at 20:46
  • Oh, ok. Well I want a graph with depth on the x axis and cover on the y axis. Then I want a line for each species showing it's cover (percentage of surface area covered by that species) at each depth. Is that clearer? – user8144422 Jun 11 '17 at 20:52
  • 1
    `summarise(mean(depth)` there is a missing `)` – Enrique Pérez Herrero Jun 11 '17 at 21:30