Movatterモバイル変換


[0]ホーム

URL:


Plotting with exuber

#> Warning: package 'dplyr' was built under R version 4.3.3#> Warning: package 'ggplot2' was built under R version 4.3.3#> Warning: package 'tidyr' was built under R version 4.3.3

The plotting API has changed in exuber 0.4.0

The new design offers full flexibility and customization to producepublication-ready plots.exuber used to plot graphindividually in a list, and then you could modify each plot and arrangethem into a single grob withggarrange()(which now isdefunct). However, newer versions of exuber focus on providing a facetedplot as it easier to change the aesthetics and themes.

Let’s start by simulating some data.

set.seed(123)sims<-tibble(sim_psy1 =sim_psy1(100),sim_psy2 =sim_psy2(100),sim_evans =sim_blan(100),sim_blan =sim_evans(100),)# Esimationestimation<-radf(sims,lag =1)# Critical Valuescrit_values<-radf_mc_cv(nrow(sims))

Same Appearance with New Features

The visual output of autoplot inv0.4.0 is exactly thesame as before.

autoplot(estimation, crit_values)

However, almost all aspects of the plot can be easily changed.

Change color and theme

The custom color for autoplot are “blue and”red”, however the usercan easily override this option withggplot2::scale_color_manual.

autoplot(estimation, crit_values)+scale_color_manual(values =c("grey","black"))+theme_classic()

Changed the shaded region with shade_opt

shade_opt allows the user to manipulate thegeom_rect() layer of the ggplot, using theshade function. Alternatively, it can be omitted if it settoNULL.

autoplot(estimation, crit_values,shade_opt =shade(fill ="pink",opacity =0.3))

Custom plotting

Custom plotting is also very easy with theaugment_join(), that merge the output of the estimation andand critical values in a ggplot2-friendly way.

joined<-augment_join(estimation, crit_values)joined#> # A tibble: 1,926 × 8#>      key index id        data stat   tstat sig    crit#>    <int> <dbl> <fct>    <dbl> <fct>  <dbl> <fct> <dbl>#>  1    21    21 sim_psy1 119.  badf  -2.08  90    -0.44#>  2    22    22 sim_psy1 112.  badf  -2.31  90    -0.44#>  3    23    23 sim_psy1 111.  badf  -2.39  90    -0.44#>  4    24    24 sim_psy1 104.  badf  -2.26  90    -0.44#>  5    25    25 sim_psy1  98.6 badf  -2.08  90    -0.44#>  6    26    26 sim_psy1  94.3 badf  -1.79  90    -0.44#>  7    27    27 sim_psy1  82.9 badf  -1.00  90    -0.44#>  8    28    28 sim_psy1  88.6 badf  -1.34  90    -0.44#>  9    29    29 sim_psy1  89.6 badf  -1.28  90    -0.44#> 10    30    30 sim_psy1  81.9 badf  -0.800 90    -0.44#> # ℹ 1,916 more rows

The output ofaugment_join returns data in tidy formatand offers full flexibility to the user. After this point plottingbecomes extremely trivial.

joined%>%ggplot(aes(x = index))+geom_line(aes(y = tstat))+geom_line(aes(y = crit))+facet_grid(sig+ stat~  id  ,scales ="free_y")#> Warning: Removed 1 row containing missing values or values outside the scale range#> (`geom_line()`).#> Removed 1 row containing missing values or values outside the scale range#> (`geom_line()`).

We also offer two functionsscale_exuber_manual andtheme_exuber that offer some extra functionality.

joined%>%pivot_longer(cols =c("tstat","crit"),names_to ="nms")%>%ggplot(aes(x = index,y = value,col = nms))+geom_line()+facet_grid(sig+ stat~  id  ,scales ="free_y")+scale_exuber_manual()+theme_exuber()

Distribution

In addition to critical values, we can also calculate the empiricaldistribution by utilizing the family of *_distr functions. For exampleif we can simulate the distribution of the supADF tests with Monte Carlomethod.

distr<-radf_mc_distr(n =300)autoplot(distr)

Empirical distribution

This part is made just for fun.

library(tidyr)distr%>%tidy()%>%rename_all(~ stringr::str_to_upper(.))%>%gather(Statistic, value,factor_key =TRUE)%>%ggplot(aes(value,color = Statistic))+stat_ecdf()+ggtitle("Empirical Cumulative Distribution")+geom_hline(yintercept =0.95,linetype ="dashed")+theme_bw()

Old Functionality

To return to the old functionality there are several ways.

library(gridExtra)#> Warning: package 'gridExtra' was built under R version 4.3.3# To choose only positive series (i.e. statistically significant for 5%)positive_series<-diagnostics(estimation, crit_values)$positive# Through a loop on positive seriesplot_list1<-list()for (asin positive_series) {  plot_list1[[as]]<-autoplot(estimation, crit_values,select_series = as)}# Alternatively  with lapplyplot_list2<-lapply(positive_series,function(x)autoplot(estimation, crit_values,select_series = x))names(plot_list2)<- positive_seriesdo.call(gridExtra::grid.arrange, plot_list1)

With the old functionality you had to make changes one at a time

plot_list1[[1]]<- plot_list1[[1]]+theme_classic()

and then reconstruct the plot withgrid.arrange or someother function that arranges all plots into a single grob.

Enjoy Plotting withexuber !!!


[8]ページ先頭

©2009-2025 Movatter.jp