簡體   English   中英

ggplot2:Plot 系數 plot 使用 facet_grid

[英]ggplot2: Plot coefficients in coefficient plot using facet_grid

我正在嘗試 plot 一些回歸的系數。 我有一個數據集,其中包含 model 特性的系數和一些變量(數據包含在帖子的末尾)。

我想 plot 使用facet_grid的系數,以便我可以 plot 安慰劑旁邊的實際效果(列),以便將效果分成結果的“類型”(行)。 問題是我還想打印每個 model 的系數值。 我想要的 output 類似於下面的內容(除了沒有顏色美學),對於每個方面

每個方面的期望輸出

我不能讓每個系數都打印在代表它們的點旁邊。 這是我嘗試過的:

# 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, 8L, 7L, 6L, 5L, 4L, 1L, 3L, 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", "Placebo using median date", "Placebo using median date", 
                                "Placebo using median date", "Placebo using median date", "Placebo using median date", 
                                "Placebo using median date", "Placebo using median date", "Effect of the protest", 
                                "Effect of the protest", "Effect of the protest", "Effect of the protest", 
                                "Effect of the protest", "Effect of the protest", "Effect of the protest", 
                                "Effect of the protest", "Effect of the protest"),
                        Model_type = structure(c(2L, 1L, 3L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 3L, 
                                                 2L, 2L, 2L, 1L, 1L, 1L), 
                                               .Label = c("Satisfaction with elite performance", 
                                                          "Trust in elites", "Placebo"), 
                                               class = "factor"),
                        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))

但是,這樣做會將每個方面中的所有系數都打印在另一個之上。 如何使 plot 的系數打印在代表它的點旁邊?

我將評論中 Z.lin 的建議解析為:

 main_plot <- main2 %>%
  ggplot(aes(x = Model, y = coef, label = 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(
    mapping = aes(label = coef),
    hjust = -0.1,
    vjust = -0.2)

main_plot

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM