繁体   English   中英

系数 plot 铲斗类别

[英]coefficient plot bucket in categories

使用 ggplot2,我想通过分类标签 A 到 J 来构造图形。 例如,A、B、C 称为“类别 1”,D、E、F 称为“类别 2”,G、H、I、J 称为“类别 3”。 与此图中所做的类似:

在此处输入图像描述

在我当前的代码下方。 如何使用 ggplot2 完成此操作? 谢谢!

  plotdata <- structure(
  list(term = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
    estimate = c(-0.033882004, 0.001508041,
      0.122957935,-0.033882004,
      0.001508041,
      -0.033882004,
      0.001508041,
      0.122957935,-0.033882004,
      0.001508041
    ),
    ymin = c(
      -0.13278953,
      -0.007547426,
      0.025116265,
      -0.13278953,
      -0.007547426,
      -0.13278953,
      -0.007547426,
      0.025116265,
      -0.13278953,
      -0.007547426
    ),
    ymax = c(
      0.065025521,
      0.010563508,
      0.220799605,
      0.065025521,
      0.010563508,
      0.065025521,
      0.010563508,
      0.220799605,
      0.065025521,
      0.010563508
    )
  ),
  row.names = c(NA,-10L),
  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)
           
         
            
            # Make plot
            p <- ggplot(plotdata, aes(x=term, y=estimate)) + 
              geom_hline(yintercept=0, color="#c10534", size=0.5) +  # Line at 0
              geom_pointrange(aes(ymin=ymin, ymax=ymax), size=0.5, color="#1a476f", shape=1, stroke = 1) +  # Ranges for each coefficient
              labs(x="", y="Coefficient Estimate", title="") +  # Labels
              coord_flip() +  # Rotate the plot
              theme_classic() +
              theme(text=element_text(size=13), axis.text.y=element_text(size=13), axis.text.x=element_text(size=13))
            p

这可以通过方面来完成:

plotdata %>%
  mutate(category = rep(paste('Category', 1:3), times = c(3, 3, 4))) %>%
  mutate(term = factor(term, levels = rev(term))) %>%
  ggplot(aes(x = term, y = estimate)) + 
  geom_hline(yintercept = 0, color="#c10534", size = 0.5) +
  geom_pointrange(aes(ymin = ymin, ymax = ymax), size = 0.5, color = "#1a476f", 
                  shape = 1, 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 = element_text(face = 'bold'),
        panel.spacing.y = unit(0, 'mm'))

在此处输入图像描述

要使标签水平,您可以执行以下操作:

plotdata %>%
  mutate(category = rep(paste('Category', 1:3), times = c(3, 3, 4))) %>%
  mutate(term = factor(term, levels = rev(term))) %>%
  ggplot(aes(x = term, y = estimate)) + 
  geom_hline(yintercept = 0, color="#c10534", size = 0.5) +
  geom_pointrange(aes(ymin = ymin, ymax = ymax), size = 0.5, color = "#1a476f", 
                  shape = 1, 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),
        panel.spacing.y = unit(0, 'mm'))

在此处输入图像描述

暂无
暂无

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

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