在R中绘制多条线

时间:2017-06-11 19:59:21

标签: r plot

我对R很陌生,所以我不知道自己在做什么。无论如何,我在excel中有这种格式的数据(作为csv文件):

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)

我想绘制一个覆盖深度的图表,每个物种都有不同的彩色线条,以及哪个物种是哪种颜色的关键。我甚至都不知道从哪里开始。

很抱歉,之前有过类似的问题。任何帮助将不胜感激!

不知道这是否有用,但这里有一些实际数据,我需要阅读更多有关dput的信息,我认为:

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")

2 个答案:

答案 0 :(得分:2)

以下是使用ggplot2包的解决方案。

# 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()

答案 1 :(得分:0)

这应该让你去!

  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()