0

Actually I have searched a lot and I could not find a solution for this problem.

I have a data like df1 and 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))

the df1 is the wavelength and the same size in lenght as 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 is the intensity and is a measurment data, Now I have tried to use plot(df1,df2) or vice versa but it does not plot it.

Roland
  • 127,288
  • 10
  • 191
  • 288
Best
  • 31
  • 7
  • Would you clarify a bit more about what the `V1` in `df1` and `V1` through `V6` in `df2` actually are. And also the type of plot you are trying to generate. – B.Shankar May 21 '15 at 15:10
  • @B.Shankar I could not put a plot because my reputation is not high. you can see a figure here http://www.blueleafsoftware.com/Products/Dagra/LEDs.php – Best May 21 '15 at 15:16
  • See if my answer produces your desired result. – B.Shankar May 21 '15 at 15:48

2 Answers2

1

This should produce the desired result:

First combine the two data sets into one:

dfd <- cbind(Wavelength = df1$V1, df2)

Now using ggplot2 graphics:

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"))

You can change the color labels to their respective actual color names by modifying the color = arguments. Note: If you want line graph as in your example, replace all geom_point to geom_line.

EDIT : A more concise solution using reshape2 package (suggested by @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()
B.Shankar
  • 1,271
  • 7
  • 11
  • I guess it would be more useful to convert `df` into the long format before using `ggplot`, otherwise you don't get any benefits from using `ggplot2` – Marat Talipov May 21 '15 at 15:58
  • @MaratTalipov, Seems interesting. But how do you think it would be useful in the present situation? Would you elaborate on this. – B.Shankar May 21 '15 at 16:04
  • 1
    Sure. For example, use `reshape2` library: `library(reshape2); z <- melt(dfd,id.vars = 'Wavelength'); ggplot(z) + aes(x=Wavelength, y=value, group=variable, colour=variable) + geom_line()` – Marat Talipov May 21 '15 at 16:07
0

You need to specify which columns you want to draw, e.g.:

plot(df1$V1,df2$V1,type='o')
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53
  • thanks but this is only ploting one column , I want to plot all columns of df 2 versus the df1 – Best May 21 '15 at 15:30
  • Do `d <- cbind(df1,df2)` and then follow the answers in http://stackoverflow.com/questions/30375600/how-to-plot-multiple-lines-for-each-column-of-a-data-matrix-against-one-column/30376071#30376071 – Marat Talipov May 21 '15 at 15:31
  • I tried but did not really produce a plot I wanted. the results are so dominated and i have no control over it . is there any simple way to plot it ??? – Best May 21 '15 at 16:03