使用效果包更改布局顺序:R
Change Layout order using Effects Package: R
我只想使用 Effects 包绘制多项式回归的一个类别。
示例:
library(nnet)
m1 <- multinom(Species ~ Sepal.Width * Petal.Width + Sepal.Length, data = iris)
plt <- effect("Sepal.Width * Petal.Width", m1, x.var = "Sepal.Width")
plot(plt, x.var = "Sepal.Width",
lattice = list(strip = list(factor.names = F)),
confint = list(style="auto", col = "black"),
axes = list(grid = T,
x = list(rug = F)))
这给了我以下信息:
https://i.stack.imgur.com/fdOw5.png
有什么方法可以只绘制,例如,class Versicolor 吗?
已经尝试使用 layout()
但我只能绘制 Setosa 和 Versicolor:
plot(plt, x.var = "Sepal.Width",
lattice = list(strip = list(factor.names = F),
layout = c(5, 2, 1)),
confint = list(style="auto", col = "black"),
axes = list(grid = T,
x = list(rug = F)))
如果我将 layout = c(5, 2, 1)
更改为 layout = c(5, 1, 1)
,它只会显示 Setosa。
谢谢!
我找到的最简单的方法是使用 ggplot2。
首先,我必须将 object
(从 effect()
)转换为数据框,然后从那里开始:
示例:
m1 <- multinom(Species ~ Sepal.Width * Petal.Width + Sepal.Length, data = iris)
plt <- effect("Sepal.Width * Petal.Width", m1)
plt_df <- data.frame(plt) # creating a data frame
并使用 ggplot2 绘图:
ggplot(data = plt_df, aes(x = Sepal.Width,
y = prob.versicolor,
ymin = L.prob.versicolor, # Lower CI
ymax = U.prob.versicolor)) + # Upper CI
geom_ribbon(alpha = 0.2) + # For CI
geom_line() +
facet_wrap(~Petal.Width, ncol = 5) +
# Few things to custoimze
scale_y_continuous("Predicted Probabilities Y",
# axes on top / right sides
sec.axis = dup_axis(),
breaks = c(0, 0.25, 0.5, .75, 1),
labels = c("0%" , "25%", "50%", "75%", "100%")) +
scale_x_continuous("Sepal Width") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
strip.placement = "outside",
## If you want to point axis ticks inwards:
### (2.75pt is the default axis tick length here)
# axis.ticks.length = unit(-2.75, "pt"),
## do not show top / right axis labels:
# axis.text.x.top = element_blank(),
## For Secondary Axis:
#axis.text.y.right = element_blank(),
## As Above, Don't show axis titles:
# axis.title.x.top = element_blank(),
axis.title.y.right = element_blank(),
# Change the distance between y axis title and ticks:
axis.title.y = element_text(margin = margin(
t = 0, r = 20, b = 0, l = 0)),
panel.background = element_blank())
https://i.stack.imgur.com/ocVJO.png
我只想使用 Effects 包绘制多项式回归的一个类别。
示例:
library(nnet)
m1 <- multinom(Species ~ Sepal.Width * Petal.Width + Sepal.Length, data = iris)
plt <- effect("Sepal.Width * Petal.Width", m1, x.var = "Sepal.Width")
plot(plt, x.var = "Sepal.Width",
lattice = list(strip = list(factor.names = F)),
confint = list(style="auto", col = "black"),
axes = list(grid = T,
x = list(rug = F)))
这给了我以下信息: https://i.stack.imgur.com/fdOw5.png
有什么方法可以只绘制,例如,class Versicolor 吗?
已经尝试使用 layout()
但我只能绘制 Setosa 和 Versicolor:
plot(plt, x.var = "Sepal.Width",
lattice = list(strip = list(factor.names = F),
layout = c(5, 2, 1)),
confint = list(style="auto", col = "black"),
axes = list(grid = T,
x = list(rug = F)))
如果我将 layout = c(5, 2, 1)
更改为 layout = c(5, 1, 1)
,它只会显示 Setosa。
谢谢!
我找到的最简单的方法是使用 ggplot2。
首先,我必须将 object
(从 effect()
)转换为数据框,然后从那里开始:
示例:
m1 <- multinom(Species ~ Sepal.Width * Petal.Width + Sepal.Length, data = iris)
plt <- effect("Sepal.Width * Petal.Width", m1)
plt_df <- data.frame(plt) # creating a data frame
并使用 ggplot2 绘图:
ggplot(data = plt_df, aes(x = Sepal.Width,
y = prob.versicolor,
ymin = L.prob.versicolor, # Lower CI
ymax = U.prob.versicolor)) + # Upper CI
geom_ribbon(alpha = 0.2) + # For CI
geom_line() +
facet_wrap(~Petal.Width, ncol = 5) +
# Few things to custoimze
scale_y_continuous("Predicted Probabilities Y",
# axes on top / right sides
sec.axis = dup_axis(),
breaks = c(0, 0.25, 0.5, .75, 1),
labels = c("0%" , "25%", "50%", "75%", "100%")) +
scale_x_continuous("Sepal Width") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
strip.placement = "outside",
## If you want to point axis ticks inwards:
### (2.75pt is the default axis tick length here)
# axis.ticks.length = unit(-2.75, "pt"),
## do not show top / right axis labels:
# axis.text.x.top = element_blank(),
## For Secondary Axis:
#axis.text.y.right = element_blank(),
## As Above, Don't show axis titles:
# axis.title.x.top = element_blank(),
axis.title.y.right = element_blank(),
# Change the distance between y axis title and ticks:
axis.title.y = element_text(margin = margin(
t = 0, r = 20, b = 0, l = 0)),
panel.background = element_blank())
https://i.stack.imgur.com/ocVJO.png