I have a matrix containing >200 data points. This is my object x. In a second object (metadata), I have a column (y) with 20 data points. I would like to plot the matrix (object x) against the 20 data points (y) in object metadata
plot(x, metadata$y)
does not work, as x and y lengths differ. Is it possible to plot this?
Matrix x:
X1 X4 X7 X9
X4 0.7
X7 0.8 0.5
X9 0.6 0.6 0.7
metadata
X1 65.4
X4 9.7
X7 47.4
X9 14.5
metadata$y: 65.4 9.7 47.4 14.5
Here's a tidyverse
solution: Alright, with these data frames (structure at bottom of post):
> df
X1 X4 X7 X9
X4 0.7 NA NA NA
X7 0.8 0.5 NA NA
X9 0.6 0.6 0.7 NA
> metadata
y
X1 65.4
X4 9.7
X7 47.4
X9 14.5
First, pull the rownames()
from metadata
for simplicity:
metadata$x <- rownames(metadata)
> metadata
y x
X1 65.4 X1
X4 9.7 X4
X7 47.4 X7
X9 14.5 X9
Now use gather
to convert the x matrix into a long format dataframe, using 'x' as the key, just as exists the rownames
step above. Next, use left_join
to join the metadata to the long dataframe, using the x
in both dataframes as the common column.
long <- gather(df, key = "x") %>%
left_join(metadata, by = "x")
> long
x value y
1 X1 0.7 65.4
2 X1 0.8 65.4
3 X1 0.6 65.4
4 X4 NA 9.7
5 X4 0.5 9.7
6 X4 0.6 9.7
7 X7 NA 47.4
8 X7 NA 47.4
9 X7 0.7 47.4
10 X9 NA 14.5
11 X9 NA 14.5
12 X9 NA 14.5
Plot:
plot(value ~ y, data = long, pch = 19)
df <-
structure(
list(
X1 = c(0.7, 0.8, 0.6),
X4 = c(NA, 0.5, 0.6),
X7 = c(NA,
NA, 0.7),
X9 = c(NA, NA, NA)
),
.Names = c("X1", "X4", "X7", "X9"),
row.names = c("X4", "X7", "X9"),
class = "data.frame"
)
metadata <-
structure(
list(y = c(65.4, 9.7, 47.4, 14.5)),
.Names = "y",
row.names = c("X1",
"X4", "X7", "X9"),
class = "data.frame"
)