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:
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:
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:
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
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.).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.