Context:有时我只需要更改绘图的标签(e.g.更改语言),但我不想再次绘制整个绘图,因为在某些情况下代码可能很长.
Problem:它们至少有两种方式来定义美学标签:
- 使用功能
ggpot2::labs(x = ..., y = ...)
和, - 带刻度函数
ggplots::scale_*_*(name = ...)
的name
参数
根据以下示例,对于绘图p
,p + labs()
似乎可以覆盖使用labs()
函数定义的实验室,但不能覆盖使用scale_*_*(name = ...)
定义的实验室.
Question:如何避免这种行为?是虫子还是我做错了什么?
Example:
这符合预期:
library(ggplot2)
# This is working as expected
p1 <- ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width)) +
scale_x_continuous() +
scale_y_continuous() +
labs(x = "A name",
y = "Another name")
p1
# trying to change the labs without making the plot again
p1 + labs(
x = "The new x title",
y = "The new y title"
)
但这不会:
library(ggplot2)
# This is not working
p2 <- ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width)) +
scale_x_continuous(name = "A name") +
scale_y_continuous(name = "Another name")
p2
p2 + labs(
x = "The new x title",
y = "The nex y title"
)