web123456

Reproducing the faceted scatterplot from the sci top issue

summary

Scientific research required for editorial directiongraphic design, I've shared a few installments on this:

  1. R Issues|Numerical Simulation Process Documentation and Sharing

  2. R language numerical experiments in the organization of common techniques

  3. ggplot plots faceted bars and labels numbers

  4. Adding different tables in facets

  5. Adding different straight lines to a faceted surface

Today I am sharing how the following graphs were implemented in the simulation experiment:

来自:Fang, G. and R. Pan (2023). "A Class of Hierarchical Multivariate Wiener Processes for Modeling Dependent Degradation Data." Technometrics: 1-16.

take note of: The graph mainly shows that for different (n,m) combinations, thestatistical inferenceestimatesperformances(root mean square error, RMSE) comparisons.

The graph is not too difficult, just need to process the data to the right format, using theggplot2 hit the nail on the headgeom_point() respond in singingfacet_wrap() can be plotted to get it. Some of these details need attention:

  1. utilizationlatex2exp packageTeX() Set the Y-axis label.

  2. Faceted theme name customization to display complex mathematical formulas.

Next, we reproduce the figure with a simulated data. The data and code for this paper can be foundGitHub

tutorials

# Importing packages
library(openxlsx)
library(ggplot2)
library(tidyverse)
library(ggsci)
library(latex2exp)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Data import

original (document etc)data structureAs shown below:

sum_dat <- ("Data summary.xlsx", sheet = 1)
  • 1

take note of: Multiple sheets can be imported.sum_dat <- sapply(1:5, function(i) ("Data aggregation.xlsx", sheet = i))

data processing

First, the rows of RMSE are extracted. Since the data has too many decimal places, we perform a scale change and multiply by 100. at this point, the data is formatted as follows

(sum_dat) %>% filter(X3 == "RMSE") -> t2_dat 
t2_dat[, 4:10] <- t2_dat[, 4:10] * 100 #Data scale changes
t2_dat$m <- rep(c(10, 20, 30), times = 3)
t2_dat$n <- rep(c(10, 20, 30), each = 3)
colnames(t2_dat) <- c("n", "m", "RMSE", "eta1", "eta2", "delta1", "delta2", "sigma1", "sigma2", "rho")
  • 1
  • 2
  • 3
  • 4
  • 5

general term "will" be used for thatdata setinto the format required by ggplot2, as shown below:

# Format conversion
dat_cal <- t2_dat %>% pivot_longer(eta1:rho, values_to = "value", names_to = "name")
dat_cal$name <- factor(dat_cal$name, levels = c("eta1", "eta2", "delta1", "delta2", "sigma1", "sigma2", "rho")) # Translating factor type
dat_cal
  • 1
  • 2
  • 3
  • 4

At this point, the required data has been prepared. Next the graph can be plotted. Before plotting, we customize the facet name, which can be found in this section:How do I add a math expression tag to a facet?

# Customized facet names
f_names <- list(
  "eta1" = TeX(c("$\\eta_{1}$")), "eta2" = TeX(c("$\\eta_{2}$")),
  "delta1" = TeX(c("$\\delta_{1}$")), "delta2" = TeX(c("$\\delta_{2}$")),
  "sigma1" = TeX(c("$\\sigma_{1}$")), "sigma2" = TeX(c("$\\sigma_{2}$")),
  "rho" = TeX(c("$\\rho_{(1,2)}$"))
)
f_labeller <- function(variable, value) {
  return(f_names[value])
}
# Set the y-axis range
y_ranges <- list(c(2, 8), c(8, 22), c(8, 20), c(15, 35), c(8, 15), c(13, 28))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

visualization

adoptiongeom_point() respond in singingfacet_wrap() Can be drawn to get. Detailed section: usingTeX(r'(RMSE($\times 10^{-2}))') Add math symbols.

p1 <- ggplot(dat_cal, aes(factor(n), value)) +
  # geom_col(aes(fill = factor(m)),position = "dodge",alpha=1) +
  geom_point(aes(color = factor(m)), size = 2) +
  facet_wrap(vars(factor(name)), scales = "free_y", labeller = f_labeller, nrow = 2) +
  theme_bw() +
  scale_color_aaas(name = "n") +
  theme( = element_blank(),  = "bottom") + 
  xlab("m") +
  ylab(TeX(r'(RMSE($\times 10^{-2}))'))
p1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10