简体   繁体   中英

coefficient plot bucket in categories

using ggplot2, I would like to structure the graphic by bucketing the labels A to J in categories. Eg A, B, C would be called "Category 1", D, E, F would be called "Category 2", and G, H, I, J would be called "Category 3". Similar as was done in this graphic:

在此处输入图像描述

Below my current code. How can I accomplish this using ggplot2? Thank you!

  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

This could be done with facets:

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'))

在此处输入图像描述

To have the labels horizontal, you could do something like:

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'))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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