【发布时间】:2019-04-15 19:54:33
【问题描述】:
我正在使用带有gg = TRUE 的visreg 包(因此它将使用ggplot2 图形)来渲染我的拟合模型图。
它自动使用自变量因子的名称作为 x 轴标签,但我需要它们看起来略有不同,并尝试使用 scale_x_discrete 更改标签文本,如 here 所示。
但是当我这样做时,x 轴标签、轴线及其标题变为空白。
我相信我无法将 labels 参数映射到 breaks 参数。
我也收到消息了
“x”的比例已经存在。为“x”添加另一个比例,它将替换现有比例。
问题可能在于visreg 如何存储变量(及其级别)信息。单独使用ggplot2 时,可以使用data$variablename 从使用过的数据集中轻松恢复此信息。但是通过visreg 创建基本情节并不是那么简单。
我已经尝试过的:
- 使用默认绘制的变量级别名称作为中断。
- 尝试使用
fit$xlevels$Species访问变量级别。 - 猜测
visreg可能将整数作为级别并尝试使用breaks = c(as.factor("1","2","3"))。
如何重现问题:
library(visreg)
library(ggplot2)
data(iris)
fit <- lm(Sepal.Length ~ Species, data = iris)
visreg(fit, gg = T) +
theme(axis.line = element_line(colour = "black")) +
scale_x_discrete(breaks = c("setosa", "versicolor", "virginica"),
labels = c("SETOSA", "VERSICOLOR", "VIRGINICA"))
# ----------------------- OR the equivalent:
visreg(fit, gg = T) +
theme(axis.line = element_line(colour = "black")) +
scale_x_discrete(labels = c("setosa" = "SETOSA",
"versicolor" = "VERSICOLOR", "virginica" = "VIRGINICA"))
不尝试更改标签的 x 轴外观:
library(visreg)
library(ggplot2)
data(iris)
fit <- lm(Sepal.Length ~ Species, data = iris)
visreg(fit, gg = T) +
theme(axis.line = element_line(colour = "black"))
【问题讨论】: