将多个功能组合到一个绘图中(ggplot) [英] Combining multiple functions into one plot (ggplot)
问题描述
我有一个(25x6)矩阵,其中包含以下观察结果(类:数据帧):
Mkt.RF SMB HML RMW CMA WML-3.86 1.37 1.14 1.47 -2.35 0.051.10 -0.95 -1.60 1.17 -0.33 -2.962.44 -1.79 0.39 1.14 -2.31 -1.559.10 2.48 0.01 -1.43 -0.12 -7.61-2.37 2.90 -0.84 0.84 -1.22 1.810.54 0.09 0.48 0.30 0.32 0.030.72 -0.48 0.40 0.20 -0.12 0.87-6.09 1.57 1.04 1.05 0.43 1.133.43 -1.63 -0.55 1.45 -0.63 3.35-1.35 0.32 -0.59 1.57 -0.80 3.432.90 0.52 0.00 -0.26 0.39 1.561.35 -0.22 -1.42 -1.58 0.19 2.25-5.10 0.77 -1.34 1.21 -0.35 1.066.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.104.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.942.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.551.78 -0.19 -1.64 -0.10 -1.17 0.69
我希望产生两个图:(1)概率密度函数,(2)ggplot中的累积分布函数.我希望每个列都有一个函数,因此应该有6个pdf和6个cdfs.我产生了以下内容:
Loaddata<-setwd(〜/Desktop")库(ggplot2)图书馆(plyr)库(reshape2)D<-read.table(file ="MyData.csv",header = TRUE,sep =;",dec =,")附上(D)因素<-cbind(D [,2:7])ggplot(faktors,aes(Mkt.RF))+ geom_density()+ labs(x ="Return",y ="Distribution",title ="PDF")+xlim(-20,20)+主题(plot.title = element_text(hjust = 0.5))
这样我可以生成一个具有单个函数(一列数据)的图,但是我很难将所有六个函数组合成一个图.这样我就可以复制类似的内容:
可以使用
创建单个图 df%>%收集(键,值)%&%;%ggplot(aes(value,color = key))+geom_density()
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:
Thank you in advance!
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")))
The single plots can be created using
df %>%
gather(key, value) %>%
ggplot(aes(value, color=key)) +
geom_density()
这篇关于将多个功能组合到一个绘图中(ggplot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!