# Plot of two different matrices in R

I'm trying to plot two different size matrices using one graph (in R), but can't manage to do so. I tried using matplot and the regular plot, but it didn't work.

Does anyone know how to plot it?

• Can you please post your matrices and your code? – aivanov Dec 18 '17 at 21:57

## 1 Answer

(Assuming this is a similar question to what was posted on Cross Validated, but was closed): You can merge the data from both matrices while adding a variable that specifies the origin (group), then plot them together in ggplot2:

library( ggplot2 )

N1 = 19
N2 = 17
M  = 10

m1 = matrix( rnorm(N1*M,mean=0,sd=1), N1, M)
m2 = matrix( rnorm(N2*M,mean=0,sd=1), N2, M)

y     = c( as.vector( t( m1 ) ),  as.vector( t( m2 ) )  )
x     = c( rep(1:10, each = N1 ), rep(1:10, each = N2 ) )
group = c( rep( '1', N1 * M ),    rep( '2', N2 * M )    )

df = data.frame( state = x, value = y, group = group )

ggplot( df, aes( x = state, y = value, colour = group ) ) +
geom_point() +
ggtitle( "State values in group 1 and 2" ) +
labs( x = "State", y = "Value" ) +
scale_x_continuous( breaks = seq(10) )


This is the result: • Wow! Thanks! Sorry for not responding earlier, I was offline for a bit. Thank you very much! – Buzi Dec 19 '17 at 11:46