向四开图添加子标题

问题描述 投票:0回答:1

我正在使用四开本写一本书,在尝试生成三个子图时,我遇到了向每个子图添加子标题的问题。这是相关的代码块:

```{r,echo=FALSE, fig.height=3, fig.width=9}
#| label: fig-plots
#| fig-cap: "Plots" 
#| fig-subcap:
#|   - "Plot 1"
#|   - "Plot 2" 
#|   - "Plot 3"
#| layout-ncol: 1

set.seed(5)
x_values <- seq(-4, 4, length.out = 1000)
a <- -sqrt(3)
b <- sqrt(3)

# Create the curve of the uniform distribution

par(mfrow = c(1,3), mar=c(5,1,5,1))
curve(dunif(x, min = a, max = b), from = -4, to = 4,
      n = 10000,
      col = "darkblue",
      lwd = 3,
      ylim = c(0, 0.45),
      ylab = '',
      xlab = 'E.K. = -1.2',
      main = expression("Uniform(min=-" * sqrt(3) * ",max=" * sqrt(3) * ")")
)
dunif_density <- function(x) dunif(x, min = a, max = b)
polygon(c(x_values, rev(x_values)), c(rep(0, length(x_values)), dunif_density(x_values)), col = 'darkblue', density = 20, border = NA)

# Create the normal distribution

curve(dnorm(x, mean = 0, sd = 1), from = -4, to = 4,
      n = 10000,
      col = 'darkblue',
      lwd = 3,
      ylim = c(0, 0.45),
      ylab = '',
      xlab = 'E.K. = 0',
      main = expression("Normal(" * mu * "=0, " * sigma * "=1)")
      )
dnorm_density <- function(x) dnorm(x, mean = 0, sd = 1)
polygon(c(x_values, rev(x_values)), c(rep(0, length(x_values)), dnorm_density(x_values)), col = 'darkblue', density = 20, border = NA)

# Create the Logistic Distribution

curve(dlogis(x, location = 0, scale = 0.55), from = -4, to = 4,
      n = 10000,
      col = 'darkblue',
      lwd = 3,
      ylim = c(0, 0.45),
      ylab = '',
      xlab = 'E.K. = 1.2',
      main = expression("Logistic(" * alpha * "=0, " * beta * "=0.55)")
      )
dlogis_density <- function(x) dlogis(x, location = 0, scale = 0.55)
polygon(c(x_values, rev(x_values)), c(rep(0, length(x_values)), dlogis_density(x_values)), col = 'darkblue', density = 20, border = NA)
```

这是结果图:

您可以清楚地看到,只有一个子标题,而不是预期的三个。这是什么原因呢?我该如何解决它?

r r-markdown quarto subfigure subcaption
1个回答
0
投票

由于您使用了

par
命令,您只能得到一个子标题,因为只有一个数字而不是三个。

---
title: "Subcaption"
format: pdf
---

## Quarto

Quarto enables you to weave together content and executable code into a finished document.

## Code

```{r}
#| label: fig-plots
#| echo: false
#| fig-cap: "Plots" 
#| fig-subcap:
#|   - "Plot 1"
#|   - "Plot 2" 
#|   - "Plot 3"
#| layout-nrow: 1

set.seed(5)
x_values <- seq(-4, 4, length.out = 1000)
a <- -sqrt(3)
b <- sqrt(3)

# Create the curve of the uniform distribution

# par(mfrow = c(1,3), mar=c(5,1,5,1))
curve(dunif(x, min = a, max = b), from = -4, to = 4,
      n = 10000,
      col = "darkblue",
      lwd = 3,
      ylim = c(0, 0.45),
      ylab = '',
      xlab = 'E.K. = -1.2',
      main = expression("Uniform(min=-" * sqrt(3) * ",max=" * sqrt(3) * ")")
)
dunif_density <- function(x) dunif(x, min = a, max = b)
polygon(c(x_values, rev(x_values)), c(rep(0, length(x_values)), dunif_density(x_values)), col = 'darkblue', density = 20, border = NA)

# Create the normal distribution

curve(dnorm(x, mean = 0, sd = 1), from = -4, to = 4,
      n = 10000,
      col = 'darkblue',
      lwd = 3,
      ylim = c(0, 0.45),
      ylab = '',
      xlab = 'E.K. = 0',
      main = expression("Normal(" * mu * "=0, " * sigma * "=1)")
      )
dnorm_density <- function(x) dnorm(x, mean = 0, sd = 1)
polygon(c(x_values, rev(x_values)), c(rep(0, length(x_values)), dnorm_density(x_values)), col = 'darkblue', density = 20, border = NA)

# Create the Logistic Distribution

curve(dlogis(x, location = 0, scale = 0.55), from = -4, to = 4,
      n = 10000,
      col = 'darkblue',
      lwd = 3,
      ylim = c(0, 0.45),
      ylab = '',
      xlab = 'E.K. = 1.2',
      main = expression("Logistic(" * alpha * "=0, " * beta * "=0.55)")
      )
dlogis_density <- function(x) dlogis(x, location = 0, scale = 0.55)
polygon(c(x_values, rev(x_values)), c(rep(0, length(x_values)), dlogis_density(x_values)), col = 'darkblue', density = 20, border = NA)
```

© www.soinside.com 2019 - 2024. All rights reserved.