1
votes

I feel like this question has been asked many times before, however from the questions I've looked at, none of the solutions so far have worked for me.

I wish to plot the values of two correlation matrices as scatter plots, next to each other in one plot with the same y range (from 0 - 1).

My original data are time series spanning several years of 112 companies, which I've split into two subsets, period A & period B. The original data is a zoo object.

I have then created the correlation matrices for both periods:

corr_A <- cor(series_A)
corr_B <- cor(series_B)

For further data analysis, I have removed the double entries:

corr_A[lower.tri(corr_A, diag=TRUE)] <- NA
corr_A <- as.vector(corr_A)
corr_A <- corr_A[!is.na(corr_A)]

corr_B[lower.tri(corr_B, diag=TRUE)] <- NA
corr_B <- as.vector(corr_B)
corr_B <- corr_B[!is.na(corr_B)]

As a result, I have two vectors, each with a length of 6216 (111 + 110 + 109 + .. + 1 = 6216).

I have then combined these vectors into a matrix:

correlation <- matrix(c(corr_A, corr_B), nrow=6216)
colnames(correlation) <- c("period_A", "period_B")

I would now like to plot this matrix so the result looks similar to this picture: Two scatter plots in one graph

I've tried to plot using xyplot from lattice:

xyplot(period_A + period_B ~ X, correlation)

However, in the resulting plot, the two scatter plots are stacked over each other:enter image description here

I have also tried changing the matrix itself - instead of using 6216 rows, I have used 12432 rows, and then index'd the first 6512 rows as "period_A" and the last 6512 rows as "period_B" - the resulting plot looks quite similar to my desired plot:

Plot 3

Is there any way I can create my desired plot using xyplot? Or are there any other (ggplot, car) methods of generating the plot?

Edit (added sample data for reproducible example):

head(correlation) #data frame with 6216 rows, 3 columns

X  period_A period_B
1    0.5     0.4
2    0.3     0.6
3    0.2     0.4
4    0.6     0.6
1
Without sample data to make a reproducible example it's very difficult to offer specific suggestions. But your desired plot really isn't in a form that's super friendly to any plotting function. It would be easiest to use a grouping variable with xyplot to make different panels (or facets with ggplot. But if you insists they be "in the same plot" then you will likely have to transform the x-values of data before plotting and then customize the scales to print the categorical levels.MrFlick
Mayve you want a violin plot?IRTFM
Looked up violin plots - don't think that's what I need. I'll add sample data to make a reproducible example.Olorun

1 Answers

1
votes

I've finally found a solution: https://stats.stackexchange.com/questions/63203/boxplot-equivalent-for-heavy-tailed-distributions

First of all, we stack the data.

correlation <- stack(correlation)

Then, we use stripplot (from lattice) combiend with jitter=TRUE to create the desired plot.

stripplot(correlation$values ~ correlation$ind, jitter=T)

The resulting plot looks exactly like my desired plot and can be manipulated using the standard lattice/plot commands (ylab, xlab, xlim, ylim,scales, etc.).