繁体   English   中英

如何使用 plot 或 ggplot2 在 R 中快速绘制多条线

[英]How to plot multiple lines quickly in R using plot or ggplot2

我有一个数据框(见下文),它有几行比通过 Fly 列标记的行(每行都是唯一的)和每行通过条件列标记的条件多。 每条线都有四个与其相关的测量值,它们是在不同时间进行的。 我想将 X 上的音量绘制为音量,将 Y 绘制为时间。 和飞行下独特线条的颜色代码。 有没有一种快速简便的方法来做到这一点? 不确定如何让它工作或从 ggplot2() 甚至 plot() 开始。 一些帮助和指导将不胜感激。

     Fly condition_1 volume_1 volume_2 volume_3 volume_4
1     1    postcont      1.6     18.0      8.1     13.5
2     2    postcont      0.0     12.0     10.1      8.5
3     3    postcont      2.6      3.0     10.1      1.5
4     4    postcont     13.6     13.0     10.1     15.5

这是绘图的代码

data1<-read.table(file=file.choose(),header=T)
head(data1)
postcontexp<-subset(data1, condition_1 == "postcont")
postcontexpVOL<- subset(# to remove unwanted columns)
postcontexpVOL<- na.omit(postcontexpVOL)

下面是一个使用版本meltreshape2dplyr以适应调整数据:

n <- 4
df <-
  data.frame(
    Ind = 1:n
    , vol_1 = rnorm(n)
    , vol_2 = rnorm(n)
    , vol_3 = rnorm(n)
    , vol_4 = rnorm(n)
  )


melted <-
  melt(df, id.vars = "Ind") %>%
  mutate(Time = as.numeric(gsub("vol_","",variable)))

ggplot(melted
       , aes(x = Time
             , y = value
             , col = as.factor(Ind))) +
  geom_line()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM