1

I have a (25x6) matrix containing the following observations (class: dataframe):

Mkt.RF   SMB   HML   RMW   CMA    WML
-3.86  1.37  1.14  1.47 -2.35   0.05
 1.10 -0.95 -1.60  1.17 -0.33  -2.96
 2.44 -1.79  0.39  1.14 -2.31  -1.55
 9.10  2.48  0.01 -1.43 -0.12  -7.61
-2.37  2.90 -0.84  0.84 -1.22   1.81
 0.54  0.09  0.48  0.30  0.32   0.03
 0.72 -0.48  0.40  0.20 -0.12   0.87
-6.09  1.57  1.04  1.05  0.43   1.13
 3.43 -1.63 -0.55  1.45 -0.63   3.35
-1.35  0.32 -0.59  1.57 -0.80   3.43
 2.90  0.52  0.00 -0.26  0.39   1.56
 1.35 -0.22 -1.42 -1.58  0.19   2.25
-5.10  0.77 -1.34  1.21 -0.35   1.06
 6.26 -1.91 -2.70  1.89 -1.94   3.01
-2.21  4.04  3.00 -0.07  1.09   0.38
-1.93  2.50  1.88  0.53  1.13   1.26
-5.48  1.04  2.45  0.79  0.61   0.90
-0.11 -1.34  2.59  3.32  2.21   0.10
 4.13  0.15  0.66 -1.51  1.13  -0.18
-3.72  0.76  0.92  0.87  0.42   2.96
-0.64 -2.35 -1.31  0.27  0.55   0.94
 2.52 -2.70 -1.71 -0.16  0.86  -3.55
-1.41 -0.20 -0.96  0.47 -0.25   2.56
-3.08 -0.45 -0.35  0.23 -2.21   1.55
 1.78 -0.19 -1.64 -0.10 -1.17   0.69

I wish to produce two plots: (1) a probability density function, and (2) a cumulative distribution function in ggplot. I would like to have a function for each column, hence there should be 6 pdfs and 6 cdfs. I have produced the following:

Loaddata <- setwd("~/Desktop")

library(ggplot2)
library(plyr)
library(reshape2)

D <- read.table(file = "MyData.csv", header = TRUE, sep =";", dec = ",")
attach(D)
factors <- cbind(D[,2:7])

ggplot(faktors, aes(Mkt.RF)) + geom_density() + labs(x = "Return", y =     "Distribution", title = "PDF")+
xlim(-20,20) + theme(plot.title = element_text(hjust = 0.5))   

With this I can produce a plot with a single function (one column of data), but I am having trouble with combining all six functions into one plot. So that I can replicate something similar to this:

PDF functions example

Thank you in advance!

2 Answers 2

1

You can try

library(tidyverse)
df %>% 
  bind_rows(df, .id="gr") %>% 
  gather(key, value, -gr) %>% 
  ggplot() + 
   geom_density(data = . %>% filter(gr == 1), aes(value, color = key), size=1.1) +
   stat_ecdf(data = . %>% filter(gr == 2), aes(value, color = key), size=1.1) +
   facet_wrap(~gr, labeller = labeller(gr=c("1" = "PD", "2" = "CD"))) 

enter image description here

The single plots can be created using

df %>% 
  gather(key, value) %>% 
  ggplot(aes(value, color=key)) + 
  geom_density() 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Do you know how to change the colors of the functions? And do you know what the pink, horizontal line on the x-axis in the pdf is?
0

Here is a reproduceable example using mtcars, and plotting all the distributions on top of eachother

library(tidyverse)

mtcars %>%
  gather(Variable, Value) %>%
  ggplot(aes(x=Value, color=Variable)) +
  geom_density(alpha=0)

output plot

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.