ggplot2:使用 facet_grid

问题描述

我正在尝试绘制一些回归的系数。我有一个数据集,其中包含模型特征的系数和一些变量(数据包含在帖子末尾)。

我想使用 facet_grid 绘制系数,以便我可以在安慰剂(列)旁边绘制实际效果,并将效果分为结果的“类型”(行)。问题是我还想打印每个模型的系数值。对于每个方面,我想要的输出类似于下面的内容(除了没有颜色美感):

Desired output,for each facet

我无法将每个系数打印在代表它们的点旁边。这是我尝试过的:

# Load necessary libraries
library(tidyverse)

anotate <- main2$coef

# Generate annotations in the plot
dat_text <- data.frame(
  label = anotate,cyl   = c(4,6,8))

# Make the plot
main_plot <- main2 %>%
  ggplot(aes(x = Model,y = coef)) +
  geom_hline(aes(yintercept = 0),linetype = 2) + 
  geom_linerange(aes(ymin = ci_lower,ymax = ci_upper),position = position_dodge(width = 0.75) ) + 
  geom_point(size = 1,position = position_dodge(width = 0.75) ) + 
  facet_grid(Model_type ~ var,scale = 'free') + 
  theme_minimal(base_size = 9) +
  coord_flip(ylim = c(-0.6,0.6)) + 
  theme(panel.background = element_rect(fill = NA,color = "black")) +
  guides(color = FALSE,shape = guide_legend(reverse = TRUE)) +
  geom_text(
    data = dat_text,mapping = aes(x = -Inf,y = -Inf,label = label),hjust = -0.1,vjust = -1)
main_plot

我的数据:

# Generate dataset
main2 <- structure(list(Model = structure(c(9L,8L,7L,6L,5L,4L,1L,3L,2L,9L,2L),.Label = c("Satisfaction w/ democracy","Satisfaction w/ economy","Satisfaction w/ government","Trust in parliament","Trust in parties","Trust in politicians","Trust in UN","Stf elite performance (PCA)","Trust in elites (PCA)"),class = "factor"),coef = c(-0.044688936,-0.046109919,-0.017885279,-0.04640542,-0.030446326,-0.058590218,-0.028425576,-0.024230592,-0.073079899,-0.38493791,-0.30536923,-0.025754517,-0.35433629,-0.38493976,-0.27348521,-0.23950855,-0.20876083,-0.22473606),var = c("Placebo using median date","Placebo using median date","Effect of the protest","Effect of the protest"),Model_type = structure(c(2L,1L),.Label = c("Satisfaction with elite performance","Trust in elites","Placebo"),ci_lower = c(-0.13866682,-0.14119312,-0.11960363,-0.13918212,-0.12342494,-0.15370362,-0.12298683,-0.1166563,-0.16764188,-0.54483932,-0.47237837,-0.24132778,-0.50503796,-0.53971964,-0.45066974,-0.41685063,-0.37277633,-0.38359925),ci_upper = c(0.049288955,0.048973277,0.083833076,0.046371285,0.062532283,0.036523174,0.066135675,0.068195112,0.02148208,-0.2250365,-0.13836008,0.18981874,-0.20363465,-0.23015988,-0.096300691,-0.062166482,-0.044745326,-0.065872885)),class = "data.frame",row.names = c(NA,-18L))

但是,这样做会将每个方面中的所有系数都打印出来。我怎样才能绘制出每个模型的系数打印在代表它的点旁边的图?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)