簡體   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