stm教程第19页,图6:主题流行率对比的图形显示
https://cran.r-project.org/web/packages/stm/vignettes/stmVignette.pdf
任何人都知道如何根据点的一侧绘制图形的文本(即“奥巴马”和“莎拉·佩林”位于点的左侧,而“布什总统”位于点的右侧)点)?
estimateEffect
函数的plot
方法不允许自定义标签定位。但是,您可以将绘图保存到对象并使用元素创建自定义绘图。您还需要添加“omit.plot=TRUE”。
p <- plot(prep, covariate = "rating", topics = c(3, 7, 20),
model = poliblogPrevFit, method = "difference",
cov.value1 = "Liberal", cov.value2 = "Conservative",
xlab = "More Conservative ... More Liberal",
main = "Effect of Liberal vs. Conservative",
xlim = c(-.1, .1), labeltype = "custom",
custom.labels = c('Obama', 'Sarah Palin','Bush Presidency'),
omit.plot=TRUE) # Add this
p
是一个列表,其中包含创建相同绘图所需的所有数据。
plot(0, 0, col = "white",
ylim=c(0,4),
ylab="", yaxt = "n",
xlim = c(-.1, .1),
xlab = "More Conservative ... More Liberal",
main = "Effect of Liberal vs. Conservative")
it <- length(p$topics)
for (i in 1:length(p$topics)) {
points(p$means[[i]], it, pch = 16)
lines(c(p$cis[[i]][1], p$cis[[i]][2]), c(it, it))
axis(2, at = it, labels = stringr::str_wrap(p$labels[i], width = 25),
las = 1, cex = 0.25, tick = FALSE,
pos = p$cis[[i]][1])
it = it - 1
}
这段代码直接来自stm包的
plotDifference
函数。请注意如何使用 axis
函数绘制刻度标签(不带轴)来完成标签。 axis(2, ...)
表示左轴。
我不确定为什么结果与图 6 不同。无论如何,要修改标签的位置,您必须告诉
axis
函数在哪一侧绘制轴(带有相应的刻度标签)。一种方法是先创建值,然后修改轴函数,如下所示:
POS <- c(1, 1, 2) # The actual position (uses ci - lower [1] or upper [2])
AXIS <- c(2, 2, 4) # which axis (2=left, 4=right)
这将告诉 R 将奥巴马和佩林的标签放在左侧(像以前一样),但将布什的标签放在右侧。现在再次运行上面的代码,除了显示的修改之外:
plot(0, 0, col = "white",
ylim=c(0,4),
ylab="", yaxt = "n",
xlim = c(-.1, .1),
xlab = "More Conservative ... More Liberal",
main = "Effect of Liberal vs. Conservative")
lines(c(0, 0), c(0, length(p$topics) + 2), lty = 2)
it <- length(p$topics)
for (i in 1:length(p$topics)) {
points(p$means[[i]], it, pch = 16)
lines(c(p$cis[[i]][1], p$cis[[i]][2]), c(it, it))
# Modify the following line
axis(AXIS[i], at = it, labels = stringr::str_wrap(p$labels[i], width = 25),
las = 1, cex = 0.25, tick = FALSE,
pos = p$cis[[i]][POS])
it = it - 1
}