更改bayesplot R软件包中mcmc_areas()绘图上的y轴文字

更改bayesplot R软件包中mcmc_areas()绘图上的y轴文字,r,ggplot2,data-visualization,stan,R,Ggplot2,Data Visualization,Stan,我在更改由mcmc_区域函数生成的图形中的y轴值时遇到一些问题。我的目标是更改文本并更新y轴值,以包括希腊字母和下标字母。我可以在使用R中的bayesplot包创建的其他图形中轻松地完成这项工作 例如: #I pull out my posterior draws posterior<-as.matrix(fit.lv2) #I grab just the parameters of interest for the moment gamma.b0<-posterior[,c('g

我在更改由
mcmc_区域
函数生成的图形中的y轴值时遇到一些问题。我的目标是更改文本并更新y轴值,以包括希腊字母和下标字母。我可以在使用R中的
bayesplot
包创建的其他图形中轻松地完成这项工作

例如:

#I pull out my posterior draws
posterior<-as.matrix(fit.lv2)

#I grab just the parameters of interest for the moment
gamma.b0<-posterior[,c('gamma[1,1]',
                       'gamma[1,2]',
                       'gamma[1,3]',
                       'gamma[1,4]')]

#I rename the columns 
colnames(gamma.b0)<-c('Intercept~(gamma[0][0])',
                      'DN~(gamma[0][1])',
                      'Pos~Events~(gamma[0][2])', 
                      'Neg~Events~(gamma[0][4])')

#And finally I plot the posterior density for each parameter of interest
b0.g1<-mcmc_dens(gamma.b0, facet_args = list(labeller=ggplot2::label_parsed))

它是一个
ggplot
对象,因此您可以

mcmc_区域(…)+ggplot2::缩放_y_离散(标签=c(“foo”、“bar”))

您需要添加
缩放y离散
,然后将现有标签映射到表达式,这些标签只是“Intercept~gamma[0][0]”。一点点努力,但它是有效的

对于上述示例,其工作原理如下:

library("ggplot2")
library("bayesplot")

mcmc_areas(gamma.b0) +  scale_y_discrete(labels=c(
  "Intercept~gamma[0][0]" = expression(paste("Intercept~",gamma[0][0])), 
  "DN~gamma[0][1]" =  expression(paste("DN~",gamma[0][1])),
  "Pos~Events~gamma[0][2]" = expression(paste("Pos~Events~",gamma[0][2])), 
  "Neg~Events~gamma[0][4]" = expression(paste("Neg~Events~",gamma[0][4]))
  ))
gamma.b0
取自问题起始点的
dput

那么看起来是这样的:


只需忽略“y”的
刻度已经存在
警告。它只是通知您,您正在覆盖比例。但是,这正是我们想要的。如果我们有完全的控制权,我们当然会替换第一个对scale_y的调用,但由于它隐藏在包中的某个地方,我们必须覆盖它。

你能给问题添加数据吗(使用
dput(后
))它是一个4000x4矩阵(仅针对我现在正在查看的参数子集)。这对我来说似乎有点大,所以我添加了数据的子集(只有250行),这实际上是我的第一次尝试之一。它返回以下错误,并且“y”的y轴为空(尽管将生成绘图)
比例已经存在。为“y”添加另一个刻度,它将替换现有的刻度。
也许您可以执行
-scale\u y\u discrete()+scale\u y\u discrete(labels=c(“foo”、“bar”)
?此警告没有问题-它只是通知您正在通过调用scale\u y\u discrete(这是您想要的)覆盖某些内容。通常,您当然会将现有调用调整为scale_y_discrete,而不是添加另一个调用来覆盖某些内容。但是在这里你不能,因为第一个调用隐藏在包中。所以一切都好。它仍然会给你情节。您还需要将新标签包装到表达式中,以实际显示所需的希腊字母(参见我的答案)。谢谢!我从来没有想到替代品会如此公开。
library("ggplot2")
library("bayesplot")

mcmc_areas(gamma.b0) +  scale_y_discrete(labels=c(
  "Intercept~gamma[0][0]" = expression(paste("Intercept~",gamma[0][0])), 
  "DN~gamma[0][1]" =  expression(paste("DN~",gamma[0][1])),
  "Pos~Events~gamma[0][2]" = expression(paste("Pos~Events~",gamma[0][2])), 
  "Neg~Events~gamma[0][4]" = expression(paste("Neg~Events~",gamma[0][4]))
  ))