使用csv在R上绘制多色图

使用csv在R上绘制多色图,r,ggplot2,R,Ggplot2,我想使用ggplot2在R上绘制我的.csv数据(我将其命名为p),但我遇到了困难 Time d d d d c m m m m c c c........... (top row of data p) 共有14行,304列。第一列是时间,其余的是d c m,依此类推……我想在一个单独的绘图窗口中,在x轴上绘制时间,在y轴上绘制剩余的303条曲线,这些303条曲线通过颜色来区分 最上面一行有d c m之类的字母。。这些是我的3个森林群:针叶林、落叶林、混交林。所以我希望所有带有“d”的图形

我想使用ggplot2在R上绘制我的
.csv
数据(我将其命名为p),但我遇到了困难

Time d d d d c m m m m c c c...........   (top row of data p)
共有14行,304列。第一列是时间,其余的是d c m,依此类推……我想在一个单独的绘图窗口中,在x轴上绘制时间,在y轴上绘制剩余的303条曲线,这些303条曲线通过颜色来区分

最上面一行有d c m之类的字母。。这些是我的3个森林群:针叶林、落叶林、混交林。所以我希望所有带有“d”的图形线都被分组成一种特定的颜色。然后用另一种颜色的“c”和另一种颜色的“m”

我找到了一种使用ggplot的方法

ggplot(p, aes(x = Time, group = 1)) +
geom_line(aes(y = d), colour="blue") +
geom_line(aes(y = c), colour = "red") +
geom_line(aes(y = m), colour = "green") +
ylab(label="NDVI") + xlab("Time")
但是从303开始,我有117列d
77代表c
109米

我应该使用什么代码,以便R通过给所有ds、cs和ms不同的颜色来绘制所有列


请帮助我,我在这件事上已经坚持了好几天。

我希望这里有一个建议能满足您的需要:

require(ggplot2)
require(reshape2)

time <- 1:14 # your 14 rows
# here data with only 3 forest types, consider using yours instead !
data.test <- data.frame(time=time, d=runif(14, max=1)*time, c=runif(14, max=2)*time, m=runif(14, max=3)*time)

#reshape your data for it to suit the ggploting
data.test <- melt(data.test, id.vars="time", variable.name="forest", value.name="y")

# add a numeric version of the factor forest (for gradient color)
data.test$numeric.forest <- rep(1:3, each=14) # replace 1:3 by 1:304

# first plot, each forest type got a color ... should not be readable with your dimensions (304 lines)
ggplot(data.test) + geom_line(aes(x=time, y=y, group=forest, color=forest))

# if you have 304 forest types, you might consider using color gradient, 
# but you need to identify a logical order in your forest, for the gradient to be informative ...
ggplot(data.test) + geom_line(aes(x=time, y=y, group=forest, color=numeric.forest), alpha=.9)
# consider playing with alpha (transparency) for your lines to be readable
require(ggplot2)
要求(2)

时间我希望这里有一个建议能满足您的需要:

require(ggplot2)
require(reshape2)

time <- 1:14 # your 14 rows
# here data with only 3 forest types, consider using yours instead !
data.test <- data.frame(time=time, d=runif(14, max=1)*time, c=runif(14, max=2)*time, m=runif(14, max=3)*time)

#reshape your data for it to suit the ggploting
data.test <- melt(data.test, id.vars="time", variable.name="forest", value.name="y")

# add a numeric version of the factor forest (for gradient color)
data.test$numeric.forest <- rep(1:3, each=14) # replace 1:3 by 1:304

# first plot, each forest type got a color ... should not be readable with your dimensions (304 lines)
ggplot(data.test) + geom_line(aes(x=time, y=y, group=forest, color=forest))

# if you have 304 forest types, you might consider using color gradient, 
# but you need to identify a logical order in your forest, for the gradient to be informative ...
ggplot(data.test) + geom_line(aes(x=time, y=y, group=forest, color=numeric.forest), alpha=.9)
# consider playing with alpha (transparency) for your lines to be readable
require(ggplot2)
要求(2)

我不清楚您的数据看起来如何,但您可以尝试将数据从宽格式转换为长格式。这将使绘图更容易。这里看几个转换的例子——哦,我在问题中提到了行和列的对立面,我有14行……304列第一列是时间,然后剩下的是我的森林类型,在森林类型(d c m)下我有ndvi值……所以基本上这个d c m是我的列标题。。。。我希望你现在能想象我的文件是什么样子的我故意这样做我的数据…有没有一种方法可以不转换为长格式而直接使用它我不清楚你的数据是什么样子的,但你可以尝试将你的数据从宽格式转换为长格式。这将使绘图更容易。这里看几个转换的例子——哦,我在问题中提到了行和列的对立面,我有14行……304列第一列是时间,然后剩下的是我的森林类型,在森林类型(d c m)下我有ndvi值……所以基本上这个d c m是我的列标题。。。。我希望你现在能想象我的文件看起来是什么样子我故意把我的数据做成这样…有没有一种方法可以让我不用转换成长格式就可以使用它呢谢谢你的帮助。我是r的新手,所以我必须努力完成一些事情。我尝试了你的方法,但在添加因子林的数字版本(用于渐变颜色)后,
$中出现了ErrorErrorError,这是因为你使用了这一行:
data.test哦,我明白了。。。非常感谢…同时,我还找到了另一种方法,并最终获得了它。首先,我改变了我的数据看起来像是转置了行和列的方式,然后我使用了像这样的简单绘图函数P非常感谢您的帮助。我是r的新手,所以我必须努力完成一些事情。我尝试了你的方法,但在添加因子林的数字版本(用于渐变颜色)后,
$中出现了ErrorErrorError,这是因为你使用了这一行:
data.test哦,我明白了。。。非常感谢…同时,我还找到了另一种方法,并最终得到了它。首先,我改变了我的数据看起来像是转置了行和列的方式,然后我使用了像这样的简单绘图函数