[英]Plotting 6 series in 1 plot in R
I have the following script: 我有以下脚本:
myfunctionSD2 <- function(mydata) { return(sd(mydata,na.rm=TRUE))}
SDmeas <- tapply(datIn$Measurement,list(as.factor(datIn$Measurement.location),as.factor(datIn$Tube.number)),myfunctionSD2)
SDMeasurement <- SDmeas[order(as.numeric(rownames(SDmeas))), ]
dput(SDMeasurement)
structure(c(0.6555264303958, 0.605687762634627, NA, 0.683435292388886,
NA, 0.615207645374612, NA, 0.739018717912613, NA, 0.79341715750565,
NA, 0.769473075819677, NA, NA, NA, NA, NA, 0.6555264303958, 0.605687762634627,
0.576875822240553, NA, 0.502849025908516, NA, 0.516028792109233,
NA, 0.486023134629369, NA, 0.489281948774758, NA, 0.464851913610455,
NA, 0.499546896146173, NA, NA, 0.6555264303958, 0.605687762634627,
NA, 0.683435292388886, NA, 0.615207645374612, NA, 0.739018717912613,
NA, 0.79341715750565, NA, 0.769473075819677, NA, NA, NA, NA,
NA, 0.6555264303958, 0.605687762634627, NA, 0.683435292388886,
NA, 0.615207645374612, NA, 0.739018717912613, NA, 0.79341715750565,
NA, 0.769473075819677, NA, 0.773437871034822, NA, NA, NA, 0.6555264303958,
0.605687762634627, NA, 0.683435292388886, NA, 0.615207645374612,
NA, 0.739018717912613, NA, 0.79341715750565, NA, 0.769473075819677,
NA, 0.773437871034822, NA, 0.760395526989779, 0.607323723612999,
0.6555264303958, 0.605687762634627, NA, 0.683435292388886, NA,
0.615207645374612, NA, 0.739018717912613, NA, 0.79341715750565,
NA, 0.769473075819677, NA, 0.773437871034822, NA, 0.760395526989779,
NA), .Dim = c(17L, 6L), .Dimnames = list(c("1", "3", "4", "5",
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
"17", "19"), c("1", "2", "3", "4", "5", "6")))
Which in clear format the table looks like this: 以清晰的格式表格如下所示:
1 2 3 4 5 6
1 0.6555264 0.6555264 0.6555264 0.6555264 0.6555264 0.6555264
3 0.6056878 0.6056878 0.6056878 0.6056878 0.6056878 0.6056878
4 NA 0.5768758 NA NA NA NA
5 0.6834353 NA 0.6834353 0.6834353 0.6834353 0.6834353
6 NA 0.5028490 NA NA NA NA
7 0.6152076 NA 0.6152076 0.6152076 0.6152076 0.6152076
8 NA 0.5160288 NA NA NA NA
9 0.7390187 NA 0.7390187 0.7390187 0.7390187 0.7390187
10 NA 0.4860231 NA NA NA NA
11 0.7934172 NA 0.7934172 0.7934172 0.7934172 0.7934172
12 NA 0.4892819 NA NA NA NA
13 0.7694731 NA 0.7694731 0.7694731 0.7694731 0.7694731
14 NA 0.4648519 NA NA NA NA
15 NA NA NA 0.7734379 0.7734379 0.7734379
16 NA 0.4995469 NA NA NA NA
17 NA NA NA NA 0.7603955 0.7603955
19 NA NA NA NA 0.6073237 NA
Here the column heads are the Tube.numbers and the row titels are the Measurement.locations. 这里的列标题是Tube.numbers,行标题是Measurement.locations。
I would like to plot each tube number (so 6 lines) into one graph with on the x axis the Measurement locations and the y axis the Standard deviation (The calculated values in the table) 我想将每个管编号(如此6行)绘制成一个图形,在x轴上测量位置,y轴标准偏差(表中的计算值)
I tried with matplot but I dont seem to be able to go around the necessity of having to have equal number of rows and columns. 我尝试使用matplot,但我似乎无法绕过必须拥有相同数量的行和列的必要性。 I would prefer to do it in a loop. 我宁愿在循环中这样做。
Can anybody help me? 有谁能够帮助我?
Much appreciated! 非常感激!
# Make an empty plot
plot(0, 0, type="n", xlim=c(1, 19), ylim=range(SDMeasurement, na.rm=TRUE))
# Add the lines
for(i in seq(df)){
y <- SDMeasurement[[i]]
lines(which(!is.na(y)), na.omit(y), col=i)
}
Another option is use ggplot2
to get an automatic legend as an extra ( since you should add it manullay with base plot). 另一个选择是使用ggplot2
来获取一个额外的自动图例(因为你应该添加manullay与基础图)。 First you put your data in the long format. 首先,您将数据放在长格式中。 I am using here using melt
from reshape2
. 我在这里使用reshape2
melt
。 Then you should remove missing values. 然后你应该删除缺失的值。 One option is to use complete.cases
. 一种选择是使用complete.cases
。 Here my code: 这是我的代码:
library(reshape2)
dat.m <- melt(dat)
library(ggplot2)
ggplot(dat.m[complete.cases(dat.m),]) +
geom_line(aes(x=Var1,y=value,color=factor(Var2))) +
xlab("location")+ylab("Measurement")+
guides(color = guide_legend(title = "Tube numbers"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.