R, address data in long-table for (gg)plot

52 views Asked by At

Sorry, can't think of a better title... still learing a lot in R. I would like to understand how to address "connected" data from a true long-table. Providing code and data below.

My code:

library(ggplot2)
set.seed(1)

Create a dataframe df1, which contains kind of aggregated data. The background is a study in which biological samples "ID" were analysed for multiple parameters/analytes, here A and B (can be many more...).

ID <- 1:12
Group <- c(1,1,1,1,2,2,2,2,3,3,3,3)
A <- runif(12,0,10)
B <- runif(12,5,20)
df1 <- data.frame(ID,Group,A,B)

Now say I have this data in a true longtable format, which would look like df2:

Analyte <- c(rep("A",12),rep("B",12))

df2 <- data.frame("ID"=rep(ID,2),"Group"=rep(Group,2),Analyte, "Value"=append(A,B))

I know that there are ways to convert data to/from longtable format. That is not the issue here...

I now want to plot the data of B on the y-Axis and A on the x-Axis, e.g.

p <- ggplot(data=df1)+
geom_point(aes(x=A,y=B,col=as.factor(Group)))
p

This works as expected

But how would I plot B vs A with the data in the form of the longtable df2 ??? That is without converting it to format df1. Is this possible at all? How could I let ggplot / geom_point know which value of B belongs to which A? E.g. via the sample ID?

Thanks!

0

There are 0 answers