how make a plot of multiple subplot in single plot.

I want to put all these 9 subplots in 1 plot. I want to get a plot like this

p1 <- ggplot(mydata, aes(CAC40)) + geom_density(fill="red")
p2 <- ggplot(mydata, aes(DAX)) + geom_density(fill="blue")
p3 <- ggplot(mydata, aes(FTSE_MIB)) + geom_density(fill="green")
p4 <- ggplot(mydata, aes(FTSE100)) + geom_density(fill="red")
p5 <- ggplot(mydata, aes(IBEX35)) + geom_density(fill="blue")
p6 <- ggplot(mydata, aes(NASDAQ_composite)) + geom_density(fill="green")
p7 <- ggplot(mydata, aes(Nikkei225)) + geom_density(fill="red")
p8 <- ggplot(mydata, aes(SENSEX)) + geom_density(fill="blue")
p9 <- ggplot(mydata, aes(SSE_composite)) + geom_density(fill="green")

Thanks in advance for the help.

data

date CAC40 DAX FTSE_MIB FTSE100 IBEX35 NASDAQ_composite NIFTY50 Nikkei225 SENSEX SSE_composite
01-08-2019 1.0271 0.976 1.218 -0.025 1.013 -0.97 -0.725 0.84 -0.987 -0.414
02-08-2019 -1.263 -1.34 -0.513 -2.344 -0.091 -0.65 0.613 -0.584 0.537 0.228
05-08-2019 -1.0296 -0.85 -0.512 -2.473 -0.53 -1.244 -0.305 -0.907 -0.386 -1.159
06-08-2019 -0.2072 -1.046 -0.877 -0.723 -0.857 0.369 1.228 1.278 1.118 0.02
07-08-2019 0.2623 0.119 -0.628 0.376 0.027 1.492 -0.936 -0.153 -0.904 -0.729
08-08-2019 1.0272 0.787 0.502 1.211 0.455 1.484 1.223 0.312 1.411 0.372
09-08-2019 -0.7622 -0.957 -0.884 -0.439 -0.639 -0.476 0.196 -0.353 0.162 -1.099
13-08-2019 1.325 1.008 1.725 0.335 0.775 2.088 -1.917 0.111 -2.111 -0.028
14-08-2019 -2.1656 -2.263 -2.458 -1.421 -1.857 -1.313 0.238 -0.072 0.21 -0.552
16-08-2019 0.8787 0.891 1.191 0.71 1.646 0.864 0.038 0.467 -0.087 0.222
19-08-2019 0.7206 0.305 1.021 1.019 0.073 -0.042 -0.369 -0.133 -0.223 1.678
20-08-2019 -0.3533 -0.517 -0.907 -0.9 -1.02 -0.511 -0.424 0.349 -0.304 0.032
21-08-2019 1.4076 1.107 1.383 1.109 0.643 0.039 -0.903 0.628 -0.639 0.169
22-08-2019 -0.4905 -0.078 0.114 -1.052 0.368 -0.59 -1.503 -0.377 -1.657 -0.146
23-08-2019 -1.5986 -1.643 -2.333 -0.466 -1.311 -2.416 1.213 0.636 0.862 0.426
26-08-2019 0.7695 0.33 0.951 -0.058 0.882 0.309 0.523 -0.317 0.348 0.44
27-08-2019 0.6961 0.786 1.247 -0.076 0.538 -1.035 -0.011 -0.054 -0.046 0.787
28-08-2019 -0.0611 -0.031 0.036 0.354 0.409 0.751 -0.497 0.025 -0.542 -0.271
29-08-2019 1.6975 1.301 1.798 0.978 0.757 0.347 -0.434 -0.193 -0.837 -0.175
30-08-2019 0.5298 0.751 -0.362 0.319 0.043 -0.652 0.323 0.305 0.297 -0.727
03-09-2019 -0.3335 -0.093 -0.182 -0.188 0.027 -0.408 -1.488 0.212 -1.664 0.144
04-09-2019 0.2383 -0.157 0.335 0.593 -0.177 0.341 0.503 0.342 0.409 1.013
05-09-2019 0.427 0.073 0.466 -0.548 0.894 0.689 -0.12 1.373 -0.481 0.444
06-09-2019 0.2132 0.377 -0.09 0.153 -0.03 -0.277 0.573 -0.011 0.533 0.099
09-09-2019 -0.3105 0.125 -0.083 -0.639 0.143 -0.535 0.607 0.49 0.476 0.032
11-09-2019 0.2074 0.14 -0.467 0.963 -0.547 0.964 0.065 0.611 0.053 -0.697
12-09-2019 0.1581 0.088 0.455 0.091 0.046 -0.148 -0.683 -0.007 -0.606 0.484
13-09-2019 0.1103 0.45 0.247 0.31 0.419 -0.169 0.811 0.367 0.563 0.072
16-09-2019 -0.4333 -0.059 -0.706 -0.626 -0.526 0.393 0.079 0.29 -0.218 -0.367
17-09-2019 0.224 0.013 -0.739 -0.014 -0.513 0.459 -1.659 0.245 -1.852 -1.508
18-09-2019 0.1913 0.23 0.74 -0.086 0.169 0.034 -0.296 -0.245 -0.157 0.053
19-09-2019 0.7366 0.824 0.742 0.578 1.027 -0.131 -1.295 -0.091 -1.421 0.213
20-09-2019 0.6261 0.044 -0.304 -0.156 0.46 -0.821 4.908 -0.233 4.969 0.055
23-09-2019 -0.8549 -0.724 -0.853 -0.256 -0.67 0.074 0.498 -0.007 0.633 -0.711
24-09-2019 -0.2858 -0.5 -0.248 -0.474 0.023 -1.885 -0.022 0.016 -0.097 0.197
25-09-2019 -0.2626 -0.13 -0.034 -0.019 -0.021 1.085 -1.078 0.266 -1.263 -0.747
26-09-2019 0.8422 0.497 0.717 0.838 0.718 -0.489 0.884 -0.507 0.747 -1.194
27-09-2019 0.2335 0.431 -0.015 1.022 0.376 -1.336 -0.38 -0.255 -0.463 0.091
30-09-2019 0.7388 0.487 0.453 -0.242 0.542 0.443 -0.145 -0.174 -0.529 -0.776
01-10-2019 -1.7305 -1.651 -1.159 -0.647 -1.198 -1.472 -1.35 0.246 -1.309 -0.486
03-10-2019 0.1245 -0.0732 0.046 -0.63 -0.376 1.095 -0.073 -0.376 -0.081 -0.622
04-10-2019 0.5771 0.456 0.449 1.099 0.197 0.936 -1.876 0.441 -1.896 -0.597
07-10-2019 0.8187 0.738 0.982 0.594 0.998 -0.002 -0.623 -0.329 -0.85 -0.478
09-10-2019 0.7508 1.006 0.559 0.326 0.438 0.099 1.438 0.452 1.461 0.785
10-10-2019 0.8707 0.673 0.936 0.278 1.189 0.585 -0.407 0.446 -0.655 0.821
11-10-2019 1.5874 2.145 1.501 0.845 1.766 -0.348 0.421 0.225 0.349 0.638
14-10-2019 -0.0576 0.143 -0.043 -0.464 0.187 0.053 0.046 0.247 0.016 0.465
15-10-2019 0.4458 0.51 0.547 -0.026 0.69 0.915 0.594 0.65 0.495 -0.486
16-10-2019 -0.1138 0.205 0.177 -0.605 0.43 0.054 -0.008 -0.03 -0.099 -0.464
17-10-2019 -0.1206 -0.003 -0.018 0.199 -0.223 -0.245 1.047 0.003 1.047 -0.089
18-10-2019 -0.2977 -0.027 -0.375 -0.441 0.209 -0.74 0.704 -0.159 0.539 -1.482
22-10-2019 0.2252 -0.024 -0.077 0.683 -0.139 -1.024 -0.59 0.142 -0.687 0.298
23-10-2019 0.6434 0.75 -0.115 0.668 0.489 0.365 0.068 0.025 -0.013 -0.384
24-10-2019 0.1069 0.089 0.362 0.931 -0.231 0.07 -0.678 0.111 -0.517 -0.105
25-10-2019 0.4785 0.063 0.242 -0.052 0.216 1.135 -0.535 0.205 -0.366 0.492
29-10-2019 0.2102 -0.052 -0.097 -0.341 -0.43 -0.439 1.227 0.102 1.37 -0.835
30-10-2019 0.2814 -0.148 -0.237 0.335 -1.066 0.238 -0.335 -0.479 -0.009 -0.344
31-10-2019 -0.7001 -0.547 0.152 -1.124 -0.417 -0.265 -0.109 0.074 -0.206 -0.536
01-11-2019 0.2158 0.379 0.878 0.745 0.376 0.616 0.034 0.529 -0.077 1.158
04-11-2019 0.6209 0.87 1.174 0.922 0.426 -0.146 0.104 0.086 0.02 0.368
05-11-2019 0.3655 0.108 0.079 0.25 -0.173 -0.141 -0.479 0.576 -0.488 0.465
06-11-2019 0.3271 0.22 -0.009 0.116 -0.095 -0.189 0.458 -0.17 0.392 -0.449
07-11-2019 0.0365 0.235 0.068 0.131 0.126 -0.244 -0.075 0.203 0.069 0.019
08-11-2019 0.3797 -0.151 0.241 -0.635 -0.263 0.625 -0.659 -0.672 -0.755 -0.995
11-11-2019 0.3419 0.214 0.154 -0.42 0.194 0.392 0.288 -0.385 0.071 -1.356
13-11-2019 -0.0181 -0.106 -0.498 -0.193 -0.817 0.32 -0.57 -0.509 -0.571 -0.267
14-11-2019 -0.0224 -0.137 -0.403 -0.794 -0.324 0.212 0.113 -0.789 0.268 0.073
15-11-2019 0.1045 -0.151 -0.046 0.138 0.426 0.192 -0.074 0.617 -0.127 -0.687
18-11-2019 -0.1658 -0.368 -0.459 0.066 -0.077 0.244 -0.257 0.483 -0.363 0.68
19-11-2019 -0.4706 -0.107 -0.812 0.22 -0.31 -0.086 0.173 -0.315 0.035 1.023
20-11-2019 0.2268 0.234 0.565 -0.837 0.018 -0.197 -0.047 -0.12 -0.192 -0.583
21-11-2019 0.3525 0.418 0.367 -0.329 0.424 -0.254 -0.476 -0.143 -0.398 0.038
22-11-2019 0.1278 0.138 -0.281 1.218 0.175 -0.125 -0.442 0.358 -0.723 -0.721
25-11-2019 0.1186 -0.008 0.296 0.949 0.214 0.851 1.269 0.053 1.112 0.712
26-11-2019 0.0373 -0.151 0.302 0.092 0.013 0.145 -0.599 -0.333 -0.491 -0.187
27-11-2019 -0.2795 -0.03 -0.589 0.361 0.098 0.411 0.267 -0.064 0.101 -0.076
28-11-2019 0.0469 -0.015 -0.252 -0.18 0.058 0.206 0.157 -0.212 -0.076 -0.437
29-11-2019 0.1574 0.433 -0.036 -0.943 0.187 -0.191 -0.742 -0.866 -0.837 -0.485
02-12-2019 -2.0876 -2.263 -2.443 -0.825 -2.251 -1.209 -0.732 0.602 -0.659 0.047
03-12-2019 -1.0354 -0.386 -0.477 -1.744 -0.448 0.708 -0.609 0.64 -0.434 0.516
04-12-2019 1.2873 1.092 1.309 0.415 1.362 0.108 0.612 -0.222 0.602 0.042
05-12-2019 -0.1155 -0.682 -0.347 -0.704 -0.441 -0.201 -0.438 0.032 -0.509 0.449
06-12-2019 0.9657 0.604 0.553 1.426 1.245 0.258 -1.045 0.029 -1.238 0.335
09-12-2019 -0.4855 -0.319 -1.111 -0.08 -0.252 -0.336 -0.013 -0.483 -0.098 0.001
10-12-2019 0.2494 0.037 0.815 -0.278 -0.369 -0.086 -0.784 0.162 -0.86 0.288
11-12-2019 0.2885 0.545 0.235 0.035 0.828 0.266 0.361 -0.125 0.316 0.062
12-12-2019 0.2224 0.411 0.697 0.793 0.554 0.832 0.23 -0.104 0.05 -0.364
13-12-2019 -0.835 -0.751 -1.621 1.099 -0.359 0.241 0.501 0.893 0.625 1.018
16-12-2019 0.7569 0.433 0.377 2.253 0.757 0.261 -0.638 -0.012 -0.559 0.452
17-12-2019 -0.3543 -0.772 0.404 0.082 -0.154 -0.069 0.683 -0.104 0.73 1.245
18-12-2019 -0.1976 -0.333 -0.072 0.206 0.163 -0.078 0.202 -0.37 0.279 -0.146
19-12-2019 0.141 -0.111 0.262 0.438 -0.056 0.546 0.297 -0.195 0.246 0.441
20-12-2019 0.7024 0.822 0.677 0.115 0.573 0.147 0.044 -0.322 -0.155 -0.487
23-12-2019 0.2629 -0.028 -0.084 0.542 0.097 -0.051 0.223 -0.419 0.227 -1.21
24-12-2019 0.0392 -0.084 0.237 0.113 0.308 -0.024 -0.446 -0.036 -0.536 0.568
26-12-2019 0.18958 0.053 0.204 0.283 0.217 0.582 -0.699 0.577 -0.915 0.876
27-12-2019 -0.0424 -0.009 -0.462 0.166 0.284 -0.474 0.599 -0.484 0.673 -0.06
30-12-2019 -0.7753 -0.394 -0.868 -0.756 -0.619 -0.649 -0.155 -0.481 -0.308 1.396
31-12-2019 0.1251 -0.092 -0.195 -0.589 -0.164 0.604 -0.642 -0.169 -0.85 0.452

I would use the facet_wrap function in ggplot2.

DF <- read.csv("~/R/Play/Dummy.csv")
library(tidyr)
library(ggplot2)

DFlong <- pivot_longer(DF, -date, names_to = "Index", values_to = "Value")
COLORS <- c(CAC40 = "red", DAX = "blue", FTSE_MIB = "green", FTSE100 = "red",
            IBEX35 = "blue", NASDAQ_composite = "green", Nikkei225 = "red",
            SENSEX = "blue", SSE_composite = "green", NIFTY50 = "orange")

ggplot(DFlong, aes(Value, fill = Index)) + geom_density() +
  scale_fill_manual(values = COLORS) +
  facet_wrap(~Index)
#> Warning: Removed 3 rows containing non-finite values (stat_density).

Created on 2020-08-09 by the reprex package (v0.3.0)

1 Like

thank you very much for your swift response.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.