英文:
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))
英文:
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 <- 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 |>
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),
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),
type = 'value',
min = -2,
max = 15,
axisLabel = list(fontSize = 25),
axisLine = list(lineStyle = list(width = 2))
) |>
e_legend(textStyle = list(fontSize = 35))
In this case the axis names goes out of the plot:
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))
答案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")
[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="10%"`
* `top=60`
* `right="10%"`
* `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. `"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 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 |>
e_charts(x) |>
e_line(z) |>
e_hide_grid_lines(which = c("x", "y")) |>
e_x_axis(
name = "X 轴名称",
nameLocation = "居中",
nameTextStyle = list(fontSize = 35, padding = c(20, 0, 0, 0)),
type = '数值类型',
min = 5,
max = 21,
axisLabel = list(fontSize = 25),
axisLine = list(onZero = FALSE, lineStyle = list(width = 1.5))
) |>
e_y_axis(
name = "Y 轴名称",
nameLocation = "居中",
nameTextStyle = list(fontSize = 35, padding = c(0, 0, 15, 0)),
type = '数值类型',
min = -2,
max = 15,
axisLabel = list(fontSize = 25),
axisLine = list(lineStyle = list(width = 1))
) |>
e_legend(textStyle = list(fontSize = 35))
英文:
Seems all you need to do is adjust the padding arguments for the x and y axis.
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(20, 0, 0, 0)),
type = 'value',
min = 5,
max = 21,
axisLabel = list(fontSize = 25),
axisLine = list(onZero = FALSE, lineStyle = list(width = 1.5))
) |>
e_y_axis(
name = "Y axis name",
nameLocation = "center",
nameTextStyle = list(fontSize = 35, padding = c(0, 0, 15, 0)),
type = 'value',
min = -2,
max = 15,
axisLabel = list(fontSize = 25),
axisLine = list(lineStyle = list(width = 1))
) |>
e_legend(textStyle = list(fontSize = 35))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论