【问题标题】:How to change axis labels using with visreg along with ggplot2如何使用 visreg 和 ggplot2 更改轴标签
【发布时间】:2019-04-15 19:54:33
【问题描述】:

我正在使用带有gg = TRUEvisreg 包(因此它将使用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"))

【问题讨论】:

    标签: r ggplot2 plot visreg


    【解决方案1】:

    我建议这个简单的解决方案:

    library(visreg)
    library(ggplot2)
    
    data(iris)
    iris$Species <- factor(iris$Species, labels=c("SETOSA", "VERSICOLOR", "VIRGINICA"))
    
    fit <- lm(Sepal.Length ~ Species, data = iris)
    visreg(fit, gg = T)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      相关资源
      最近更新 更多