如何在echarts4r图表周围添加边距以适应轴名称?

huangapple go评论68阅读模式
英文:

How to add margins around echarts4r plots to fit axis names?

问题

我正在尝试使用echarts4r绘制图表,但当我添加轴标签时,它们要么太靠近图表,要么在添加填充时位于图表之外。这个问题也在我尝试将图表添加到quarto revealjs演示文稿时出现。是否有可能增加图表周围的边距,以便容纳轴标签?下面是一个可复制的示例:

我的数据

df <- data.frame(
  x = seq(50),
  y = rnorm(50, 10, 3),
  z = rnorm(50, 11, 2),
  w = rnorm(50, 9, 2)
)

这个示例可以工作,但轴标签和轴名称重叠:

library(echarts4r)
df |>
  e_charts(x) |>
  e_line(z) |>
  e_hide_grid_lines(which = c("x", "y")) |>
  e_x_axis(
    name = "X轴名称",
    nameLocation = "center",
    nameTextStyle = list(fontSize = 35),
    type = 'value',
    min = 5, 
    max = 21, 
    axisLabel = list(fontSize = 25),
    axisLine = list(onZero = FALSE, lineStyle = list(width = 2))
  ) |>
  e_y_axis(
    name = "Y轴名称",
    nameLocation = "center",
    nameTextStyle = list(fontSize = 35),
    type = 'value',
    min = -2, 
    max = 15, 
    axisLabel = list(fontSize = 25),
    axisLine = list(lineStyle = list(width = 2))
  ) |>
  e_legend(textStyle = list(fontSize = 35))

在这种情况下,轴名称超出了图表:

df |>
  e_charts(x) |>
  e_line(z) |>
  e_hide_grid_lines(which = c("x", "y")) |>
  e_x_axis(
    name = "X轴名称",
    nameLocation = "center",
    nameTextStyle = list(fontSize = 35, padding = c(30, 0, 0, 0)),
    type = 'value',
    min = 5, 
    max = 21, 
    axisLabel = list(fontSize = 25),
    axisLine = list(onZero = FALSE, lineStyle = list(width = 2))
  ) |>
  e_y_axis(
    name = "Y轴名称",
    nameLocation = "center",
    nameTextStyle = list(fontSize = 35, padding = c(0, 0, 30, 0)),
    type = 'value',
    min = -2, 
    max = 15, 
    axisLabel = list(fontSize = 25),
    axisLine = list(lineStyle = list(width = 2))
  ) |>
  e_legend(textStyle = list(fontSize = 35))

如何在echarts4r图表周围添加边距以适应轴名称?

如何在echarts4r图表周围添加边距以适应轴名称?

英文:

I am trying to make a plot using echarts4r and when I add axis labels they are either too close or outside of the plot when padding is added. This issue also occurs when I try to add the plot to a quarto revealjs presentation. Is it possible to increase the margins around the plot so that it can fit the axes labels? A reproducible example is below:

My data

df &lt;- data.frame(
  x = seq(50),
  y = rnorm(50, 10, 3),
  z = rnorm(50, 11, 2),
  w = rnorm(50, 9, 2)
)

This works but the axis labels and axis names are overlapping:

library(echarts4r)
df |&gt;
  e_charts(x) |&gt;
  e_line(z) |&gt;
  e_hide_grid_lines(which = c(&quot;x&quot;, &quot;y&quot;)) |&gt;
  e_x_axis(
    name = &quot;X axis name&quot;,
    nameLocation = &quot;center&quot;,
    nameTextStyle = list(fontSize = 35),
    type = &#39;value&#39;,
    min = 5, 
    max = 21, 
    axisLabel = list(fontSize = 25),
    axisLine = list(onZero = FALSE, lineStyle = list(width = 2))
  ) |&gt;
  e_y_axis(
    name = &quot;Y axis name&quot;,
    nameLocation = &quot;center&quot;,
    nameTextStyle = list(fontSize = 35),
    type = &#39;value&#39;,
    min = -2, 
    max = 15, 
    axisLabel = list(fontSize = 25),
    axisLine = list(lineStyle = list(width = 2))
  ) |&gt;
  e_legend(textStyle = list(fontSize = 35))

如何在echarts4r图表周围添加边距以适应轴名称?

In this case the axis names goes out of the plot:

df |&gt;
  e_charts(x) |&gt;
  e_line(z) |&gt;
  e_hide_grid_lines(which = c(&quot;x&quot;, &quot;y&quot;)) |&gt;
  e_x_axis(
    name = &quot;X axis name&quot;,
    nameLocation = &quot;center&quot;,
    nameTextStyle = list(fontSize = 35, padding = c(30, 0, 0, 0)),
    type = &#39;value&#39;,
    min = 5, 
    max = 21, 
    axisLabel = list(fontSize = 25),
    axisLine = list(onZero = FALSE, lineStyle = list(width = 2))
  ) |&gt;
  e_y_axis(
    name = &quot;Y axis name&quot;,
    nameLocation = &quot;center&quot;,
    nameTextStyle = list(fontSize = 35, padding = c(0, 0, 30, 0)),
    type = &#39;value&#39;,
    min = -2, 
    max = 15, 
    axisLabel = list(fontSize = 25),
    axisLine = list(lineStyle = list(width = 2))
  ) |&gt;
  e_legend(textStyle = list(fontSize = 35))

如何在echarts4r图表周围添加边距以适应轴名称?

答案1

得分: 1

你可以通过e_grid来增加绘图周围的边距。

文档中,默认情况下边距设置为:

  • left="10%"
  • top=60
  • right="10%"
  • bottom=60

因此,为了考虑轴名称周围的填充,你可以将bottom边距增加到90,将left增加到例如"15%"

library(echarts4r)

set.seed(123)

df %>%
  e_charts(x) %>%
  e_line(z) %>%
  e_hide_grid_lines(which = c("x", "y")) %>%
  e_x_axis(
    name = "X轴名称",
    nameLocation = "center",
    nameTextStyle = list(
      fontSize = 35,
      padding = c(30, 0, 0, 0)
    ),
    type = "value",
    min = 5,
    max = 21,
    axisLabel = list(fontSize = 25),
    axisLine = list(onZero = FALSE, lineStyle = list(width = 2))
  ) %>%
  e_y_axis(
    name = "Y轴名称",
    nameLocation = "center",
    nameTextStyle = list(
      fontSize = 35, padding = c(0, 0, 30, 0)
    ),
    type = "value",
    min = -2,
    max = 15,
    axisLabel = list(fontSize = 25),
    axisLine = list(lineStyle = list(width = 2))
  ) %>%
  e_legend(textStyle = list(fontSize = 35)) %>%
  e_grid(left = "15%", bottom = "90")

如何在echarts4r图表周围添加边距以适应轴名称?


[1]: https://i.stack.imgur.com/oiIZo.png

<details>
<summary>英文:</summary>

You could increase the margin around the plot via `e_grid`.

From the 
(https://echarts.apache.org/en/option.html#grid.left) the margins are by default set to * `left=&quot;10%&quot;` * `top=60` * `right=&quot;10%&quot;` * `bottom=60` Hence, to account of the padding around the axis name you could increase the `bottom` margin to `90` and on the `left` to e.g. `&quot;15%&quot;` :

library(echarts4r)

set.seed(123)

df |>
e_charts(x) |>
e_line(z) |>
e_hide_grid_lines(which = c("x", "y")) |>
e_x_axis(
name = "X axis name",
nameLocation = "center",
nameTextStyle = list(
fontSize = 35,
padding = c(30, 0, 0, 0)
),
type = "value",
min = 5,
max = 21,
axisLabel = list(fontSize = 25),
axisLine = list(onZero = FALSE, lineStyle = list(width = 2))
) |>
e_y_axis(
name = "Y axis name",
nameLocation = "center",
nameTextStyle = list(
fontSize = 35, padding = c(0, 0, 30, 0)
),
type = "value",
min = -2,
max = 15,
axisLabel = list(fontSize = 25),
axisLine = list(lineStyle = list(width = 2))
) |>
e_legend(textStyle = list(fontSize = 35)) |>
e_grid(left = "15%", bottom = "90")


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/oiIZo.png

</details>



# 答案2
**得分**: 0

似乎你只需要调整 x 和 y 轴的 padding 参数。

```R
df |&gt;
    e_charts(x) |&gt;
    e_line(z) |&gt;
    e_hide_grid_lines(which = c(&quot;x&quot;, &quot;y&quot;)) |&gt;
    e_x_axis(
        name = &quot;X 轴名称&quot;,
        nameLocation = &quot;居中&quot;,
        nameTextStyle = list(fontSize = 35, padding = c(20, 0, 0, 0)),
        type = &#39;数值类型&#39;,
        min = 5, 
        max = 21, 
        axisLabel = list(fontSize = 25),
        axisLine = list(onZero = FALSE, lineStyle = list(width = 1.5))
    ) |&gt;
    e_y_axis(
        name = &quot;Y 轴名称&quot;,
        nameLocation = &quot;居中&quot;,
        nameTextStyle = list(fontSize = 35, padding = c(0, 0, 15, 0)),
        type = &#39;数值类型&#39;,
        min = -2, 
        max = 15, 
        axisLabel = list(fontSize = 25),
        axisLine = list(lineStyle = list(width = 1))
    ) |&gt;
    e_legend(textStyle = list(fontSize = 35))
英文:

Seems all you need to do is adjust the padding arguments for the x and y axis.

df |&gt;
    e_charts(x) |&gt;
    e_line(z) |&gt;
    e_hide_grid_lines(which = c(&quot;x&quot;, &quot;y&quot;)) |&gt;
    e_x_axis(
        name = &quot;X axis name&quot;,
        nameLocation = &quot;center&quot;,
        nameTextStyle = list(fontSize = 35, padding = c(20, 0, 0, 0)),
        type = &#39;value&#39;,
        min = 5, 
        max = 21, 
        axisLabel = list(fontSize = 25),
        axisLine = list(onZero = FALSE, lineStyle = list(width = 1.5))
    ) |&gt;
    e_y_axis(
        name = &quot;Y axis name&quot;,
        nameLocation = &quot;center&quot;,
        nameTextStyle = list(fontSize = 35, padding = c(0, 0, 15, 0)),
        type = &#39;value&#39;,
        min = -2, 
        max = 15, 
        axisLabel = list(fontSize = 25),
        axisLine = list(lineStyle = list(width = 1))
    ) |&gt;
    e_legend(textStyle = list(fontSize = 35))

huangapple
  • 本文由 发表于 2023年2月24日 07:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定