繁体   English   中英

整理系数plot

[英]Tidy up coefficient plot

在下面的系数 plot 中,我想减少“道路”和“时间”下的空白区域,以红色突出显示: 在此处输入图像描述

我怎样才能让这个看起来更整洁? 以下是我当前的代码,使用 ggplot2:

plotdata <- structure(
  list(
    term = c("Category 1_a","Category 1_b","Category 1_c","Category 1_d","Category 2",
      "Category 3_a","Category 3_b"),
    estimate = c(-0.033882004,0.001508041,0.122957935,-0.033882004,0.001508041,-0.033882004,0.001508041),
    ymin = c(
      -0.13278953,-0.007547426,0.025116265,-0.13278953,-0.007547426,-0.13278953,-0.007547426), ymax = c(0.065025521,0.010563508,0.220799605,0.065025521,0.010563508,0.065025521,0.010563508)
  ), row.names = c(NA,-7L),
  spec = structure(list(cols = list(
      term = structure(list(), class = c("collector_character",
                                         "collector")),
      estimate = structure(list(), class = c("collector_double",
                                             "collector")),
      ymin = structure(list(), class = c("collector_double",
                                         "collector")),
      ymax = structure(list(), class = c("collector_double",
                                         "collector"))
    ),
    default = structure(list(), class = c("collector_guess",
                                          "collector")),
    delim = ","), class = "col_spec"),class = c("spec_tbl_df","tbl_df", "tbl", "data.frame"))

# Libraries
library(dplyr)
library(broom)
library(ggplot2)

# Plot
plotdata %>%
  mutate(category = rep(c("Pollution", "Roads", "Timing"), times = c(4, 1, 2))) %>%
  mutate(term = factor(term, levels = rev(term))) %>%
  ggplot(aes(x = term, y = estimate)) + 
  geom_hline(yintercept = 0, color="black", size = 0.5, linetype = "dashed") +
  geom_pointrange(aes(ymin = ymin, ymax = ymax), size = 0.5, 
                  fill="#0202fe", color="#b5b5b5", shape=21, stroke = 1) +
  labs(x = NULL, y = "Coefficient Estimate") +
  scale_x_discrete(expand = c(0.4, 0.1)) +
  coord_flip() +
  facet_grid(category~., switch = 'y', scales = 'free_y') +
  theme_classic(base_size = 13) +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        strip.text.y.left = element_text(face = 'bold', angle = 0, vjust = 1, hjust = 1),
        panel.spacing.y = unit(0, 'mm'))

这可以通过facet_gridspace参数来实现。 设置space="free_y"会根据“与y标尺长度成正比”设置面板大小,即类别个数:

注意:我还删除了scale_x_discrete

library(dplyr)
library(broom)
library(ggplot2)

# Plot
plotdata %>%
  mutate(category = rep(c("Pollution", "Roads", "Timing"), times = c(4, 1, 2))) %>%
  mutate(term = factor(term, levels = rev(term))) %>%
  ggplot(aes(x = term, y = estimate)) + 
  geom_hline(yintercept = 0, color="black", size = 0.5, linetype = "dashed") +
  geom_pointrange(aes(ymin = ymin, ymax = ymax), size = 0.5, 
                  fill="#0202fe", color="#b5b5b5", shape=21, stroke = 1) +
  labs(x = NULL, y = "Coefficient Estimate") +
  coord_flip() +
  facet_grid(category~., switch = 'y', scales = 'free_y', space = "free_y") +
  theme_classic(base_size = 13) +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        strip.text.y.left = element_text(face = 'bold', angle = 0, vjust = 1, hjust = 1),
        panel.spacing.y = unit(0, 'mm'))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM