using ggplot2, I would like to structure the graphic by bucketing the labels A to J in categories. E.g. 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


