I have a data manipulation/table joining question that I have been hitting my head against for a couple of days. I am trying to create plots using ggplot2 that color the data by factors.
The simple way to do this is by using:
ggplot(data, aes(X,Y)) +
geom_point(aes(color = Factor_A))
This means I need a table that has a column for X, Y, and Factor_A.
However, my factor data and my xy data are in two completely different forms. My factor data is neatly framed like so:
factor_data <-data.frame(
Sample_ID = c(1:12),
Factor_A = sample(letters[3:6],12,replace=TRUE),
Factor_B = sample(letters[7:8],12,replace=TRUE)
)
Sample_ID, Factor_A, and Factor_B each have their own vertical column. So far, great for plotting.
However, my X,Y data is framed like so:
xy_data <-data.frame(
X = c((1:80)/10),
"1" = rnorm(80),
"2" = rnorm(80),
"3" = rnorm(80),
"4" = rnorm(80),
"5" = rnorm(80),
"6" = rnorm(80),
"7" = rnorm(80),
"8" = rnorm(80),
"9" = rnorm(80),
"10" = rnorm(80),
"11" = rnorm(80),
"12" = rnorm(80),
check.names = FALSE
)
In this case, each sample ID is horizontally across the top row. Each Sample_ID has its own X,Y spectrum (with the same X values shared across all of them). I am trying to plot all the spectra (in this simplified example, 12 spectra) at once and color each line by one of the Factors.
Does anyone have an idea as to how to join these tables together to make it possible to plot the X,Y data using ggplot2 and still be able to color the plotted lines using Factor_A or Factor_B?