如何在绘图前不过滤数据的情况下在 ggplot2 中绘制 plot_model x 轴项目的子集?
How to plot a subset of plot_model x-axis items in ggplot2 without filtering data before plotting?
我想用sjPlot::plot_model
生成一些边际效应图,稍后稍作修改。具体来说,我想 运行 使用有序分类预测变量进行回归。然后我想 运行 plot_model
在回归对象上生成一个 ggplot2
对象但是为了便于可视化而删除分类预测变量的一些元素(例如,从回归中的 10 个类别到图中的 5 个类别)。
我知道我可以使用 ggeffects::ggpredict()
来生成 plot_model
使用的基础数据,但希望有更简单的方法,比如将参数传递给 scale_x_discrete()
。
这是标准的 plot_model
输出。有没有一种直接的方法可以删除 x 轴元素之一,如“6”,但仍绘制“4”和“8”?
library(sjPlot)
mt <- mtcars
mt$cyl_fct <- as.factor(mt$cyl)
# automatic transmission vs number of cylinders
glm_out <- glm(am ~ cyl_fct, family = binomial, data = mt)
# plot model works fine but how to just show just 4 and 8 on x-axis?
plot_model(glm_out, type = "eff", terms = "cyl_fct") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1))
# options like `breaks` and `limits` don't seem to do the trick
plot_model(glm_out, type = "eff", terms = "cyl_fct") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1)) +
scale_x_discrete(breaks = c("4", "8"), limits = c("4", "8"))
您可以在 plot_model 函数中指定要绘制的项,例如
library(tidyverse)
library(sjPlot)
mt <- mtcars
mt$cyl_fct <- as.factor(mt$cyl)
# automatic transmission vs number of cylinders
glm_out <- glm(am ~ cyl_fct, family = binomial, data = mt)
# plot model works fine but how to just show just 4 and 8 on x-axis?
plot_model(glm_out, type = "eff", terms = "cyl_fct[4, 8]") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1))
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.
由 reprex package (v2.0.0)
于 2021-07-21 创建
--
如果这看起来有点奇怪,因为术语应该是“差距”,您可以调整 x 轴比例以适应,例如
plot_model(glm_out, type = "eff", terms = "cyl_fct[4, 8]") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1)) +
scale_x_discrete(breaks = c(4, 8), limits = c(4, 8))
我想用sjPlot::plot_model
生成一些边际效应图,稍后稍作修改。具体来说,我想 运行 使用有序分类预测变量进行回归。然后我想 运行 plot_model
在回归对象上生成一个 ggplot2
对象但是为了便于可视化而删除分类预测变量的一些元素(例如,从回归中的 10 个类别到图中的 5 个类别)。
我知道我可以使用 ggeffects::ggpredict()
来生成 plot_model
使用的基础数据,但希望有更简单的方法,比如将参数传递给 scale_x_discrete()
。
这是标准的 plot_model
输出。有没有一种直接的方法可以删除 x 轴元素之一,如“6”,但仍绘制“4”和“8”?
library(sjPlot)
mt <- mtcars
mt$cyl_fct <- as.factor(mt$cyl)
# automatic transmission vs number of cylinders
glm_out <- glm(am ~ cyl_fct, family = binomial, data = mt)
# plot model works fine but how to just show just 4 and 8 on x-axis?
plot_model(glm_out, type = "eff", terms = "cyl_fct") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1))
# options like `breaks` and `limits` don't seem to do the trick
plot_model(glm_out, type = "eff", terms = "cyl_fct") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1)) +
scale_x_discrete(breaks = c("4", "8"), limits = c("4", "8"))
您可以在 plot_model 函数中指定要绘制的项,例如
library(tidyverse)
library(sjPlot)
mt <- mtcars
mt$cyl_fct <- as.factor(mt$cyl)
# automatic transmission vs number of cylinders
glm_out <- glm(am ~ cyl_fct, family = binomial, data = mt)
# plot model works fine but how to just show just 4 and 8 on x-axis?
plot_model(glm_out, type = "eff", terms = "cyl_fct[4, 8]") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1))
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.
由 reprex package (v2.0.0)
于 2021-07-21 创建-- 如果这看起来有点奇怪,因为术语应该是“差距”,您可以调整 x 轴比例以适应,例如
plot_model(glm_out, type = "eff", terms = "cyl_fct[4, 8]") +
scale_y_continuous(labels = scales::percent_format(accuracy = 0.1)) +
scale_x_discrete(breaks = c(4, 8), limits = c(4, 8))