其实我已经搜索了很多,我找不到解决这个问题的方法。
我有像df1和df2
这样的数据df1<- structure(list(V1 = c(925.2, 929.055, 932.91, 936.765, 940.62,
944.475, 948.33, 952.185, 956.04, 959.895, 963.75, 967.605, 971.46,
975.315, 979.17, 983.025, 986.88, 990.735, 994.59, 998.445, 1002.3,
1006.155, 1010.01)), .Names = "V1", class = "data.frame", row.names = c(NA,
-23L))
df1是波长,长度与df2相同。
df2<- structure(list(V1 = c(1.2138218, 1.20229547, 1.19370594, 1.18406585,
1.17474114, 1.1659916, 1.15794742, 1.1504168, 1.14266249, 1.13331046,
1.12056388, 1.1027684, 1.07916047, 1.05045274, 1.01889485, 0.98764698,
0.95964186, 0.93640246, 0.91734136, 0.89985375, 0.88016878, 0.8546364,
0.8210004), V2 = c(1.1944769, 1.2044156, 1.1958914, 1.1863521,
1.177123, 1.1683985, 1.1602467, 1.1524525, 1.1443126, 1.1345368,
1.1214308, 1.1034221, 1.0797777, 1.0511821, 1.0198132, 0.9887466,
0.9608482, 0.9376109, 0.9184564, 0.9008158, 0.880963, 0.855286,
0.8215516), V3 = c(1.192471, 1.1854642, 1.1773381, 1.1682578,
1.1595011, 1.1513092, 1.1438045, 1.1367952, 1.1295544, 1.1207314,
1.1085633, 1.0914286, 1.0685784, 1.040708, 1.0100161, 0.9795931,
0.9523085, 0.9296534, 0.9110514, 0.8939461, 0.8746324, 0.8495194,
0.8163846), V4 = c(1.2000113, 1.1833215, 1.1753832, 1.166816,
1.1587624, 1.1512878, 1.1443524, 1.1376584, 1.1304499, 1.1214213,
1.1089041, 1.0913883, 1.0682224, 1.0401648, 1.0094345, 0.9791035,
0.9519961, 0.9295479, 0.9111335, 0.8941637, 0.8749218, 0.8498227,
0.8166596), V5 = c(1.202633, 1.1907639, 1.1825598, 1.1735177,
1.1648682, 1.1567665, 1.1492625, 1.142129, 1.1346457, 1.125498,
1.1129647, 1.0954514, 1.0722142, 1.0439373, 1.0128083, 0.9819197,
0.9541607, 0.9310512, 0.912038, 0.8945685, 0.8749184, 0.8494622,
0.8159423), V6 = c(1.215689, 1.1933457, 1.1850756, 1.1759209,
1.1671304, 1.1588694, 1.1511887, 1.1438567, 1.1361504, 1.126759,
1.1139725, 1.0962136, 1.0727591, 1.0443134, 1.0130784, 0.9821486,
0.9543992, 0.9313219, 0.9123303, 0.894846, 0.8751373, 0.8495936,
0.8159881)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6"), class = "data.frame", row.names = c(NA,
-23L))
df2是强度,是测量数据, 现在我尝试使用plot(df1,df2),反之亦然,但它没有绘制它。
答案 0 :(得分:1)
这应该产生预期的结果:
首先将两个数据集合并为一个:
dfd <- cbind(Wavelength = df1$V1, df2)
现在使用ggplot2图形:
library(ggplot2) # install.packages("ggplot2")
ggplot(dfd, aes(x = Wavelength)) +
geom_point(aes(y = V1, color = "V1")) +
geom_point(aes(y = V2, color = "V2")) +
geom_point(aes(y = V3, color = "V3")) +
geom_point(aes(y = V4, color = "V4")) +
geom_point(aes(y = V5, color = "V5")) +
geom_point(aes(y = V6, color = "V6"))
您可以通过修改color =
参数将颜色标签更改为各自的实际颜色名称。
注意:如果您想要示例中的折线图,请将所有geom_point
替换为geom_line
。
编辑: 使用 reshape2 包的更简洁的解决方案(由@Marat Talipov建议):
library(reshape2)
library(ggplot2)
z <- melt(dfd,id.vars = 'Wavelength')
ggplot(z) +
aes(x=Wavelength, y=value, group=variable, color=variable) +
geom_line()
答案 1 :(得分:0)
您需要指定要绘制的列,例如:
plot(df1$V1,df2$V1,type='o')