简体   繁体   English

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

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

I have a dataframe (see below) that has several lines more than shown labeled via the Fly column (each is unique) and a condition that each line received labeled via the condition column.我有一个数据框(见下文),它有几行比通过 Fly 列标记的行(每行都是唯一的)和每行通过条件列标记的条件多。 Each line has four measurements associated with it taken at different times.每条线都有四个与其相关的测量值,它们是在不同时间进行的。 I want to plot volume on the X as volume and Y as time.我想将 X 上的音量绘制为音量,将 Y 绘制为时间。 and color code by the unique line under Fly.和飞行下独特线条的颜色代码。 Is there a quick and easy way to do this?有没有一种快速简便的方法来做到这一点? Not to sure how to get this to work or started with ggplot2() or even plot().不确定如何让它工作或从 ggplot2() 甚至 plot() 开始。 Some help and guidance would be appreciated.一些帮助和指导将不胜感激。

     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

Here is the code up to plotting这是绘图的代码

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)

Here is a version using melt from reshape2 and dplyr to fit adjust the data:下面是一个使用版本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