Importing libr.
reg_results_holding_periods_coefficients <- structure(list(estimate = c(0.285600060160589, 0.217927116290694,
0.134701599627727, 0.0896591006174163, 0.0734158463083404, 0.0608466549826728,
0.0486097030045517, 0.0401007072295853, 0.0308951206105791, 0.0253381679953092,
0.021513615579265, 0.661133167454535, 0.431578140877729, 0.265876430935115,
0.183658670199874, 0.138233507075241, 0.124403086427076, 0.0981645433309161,
0.11044124019981, 0.088975622267333, 0.0870994132668995, 0.0793922440017489,
-0.358219106875326, -0.112964312762333, -0.214750913450474, -0.148208470796826,
-0.136634845269832, -0.10278907400666, -0.0861694805522434, -0.0780928197988248,
-0.0717341952873777, -0.0489804255538236, -0.0280664342536037
), sample_sizes = c("Control group", "Control group", "Control group",
"Control group", "Control group", "Control group", "Control group",
"Control group", "Control group", "Control group", "Control group",
"Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1",
"Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1",
"Treatment 1", "Treatment 2", "Treatment 2", "Treatment 2", "Treatment 2",
"Treatment 2", "Treatment 2", "Treatment 2", "Treatment 2", "Treatment 2",
"Treatment 2", "Treatment 2"), year = c("< 1", "1-2", "2-3",
"3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10", "> 10", "< 1",
"1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10",
"> 10", "< 1", "1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8",
"8-9", "9-10", "> 10"), num_of_obs = c(11148L, 5339L, 5351L,
4995L, 5001L, 4823L, 4427L, 3965L, 3357L, 3128L, 37108L, 1446L,
551L, 342L, 262L, 225L, 198L, 150L, 139L, 97L, 87L, 1003L, 59L,
111L, 311L, 455L, 598L, 559L, 513L, 412L, 340L, 287L, 1548L),
row = 1:33, lower_ci = c(0.276899313667802, 0.208715239159497,
0.12929418479355, 0.0855358716610185, 0.0703639637264808,
0.0581478912016657, 0.0461085497023501, 0.0376386484961451,
0.0285457550948043, 0.0230553955578293, 0.0210421131537736,
0.635920530584401, 0.399007766963874, 0.242890856713076,
0.162910489271237, 0.121801854572761, 0.107986009329363,
0.0837232007680086, 0.0960924603750846, 0.071918513411995,
0.0724287431302565, 0.0754971216683171, -0.43656918503493,
-0.164705377781974, -0.235877113010794, -0.16269415680847,
-0.145726963787727, -0.111130412801045, -0.0934266578936087,
-0.0864271623965783, -0.0796390193104697, -0.0559375450272804,
-0.0305204141137063), upper_ci = c(0.294300806653376, 0.22713899342189,
0.140109014461903, 0.0937823295738141, 0.0764677288902001,
0.06354541876368, 0.0511108563067534, 0.0425627659630255,
0.033244486126354, 0.0276209404327892, 0.0219851180047563,
0.686345804324669, 0.464148514791584, 0.288862005157155,
0.20440685112851, 0.15466515957772, 0.140820163524789, 0.112605885893824,
0.124790020024536, 0.106032731122671, 0.101770083403543,
0.0832873663351808, -0.279869028715723, -0.0612232477426919,
-0.193624713890155, -0.133722784785182, -0.127542726751936,
-0.0944477352122748, -0.0789123032108781, -0.0697584772010714,
-0.0638293712642856, -0.0420233060803668, -0.0256124543935011
)), class = "data.frame", row.names = c("`< 1 year`", "`1-2 years`",
"`2-3 years`", "`3-4 years`", "`4-5 years`", "`5-6 years`", "`6-7 years`",
"`7-8 years`", "`8-9 years`", "`9-10 years`", "`> 10 years`",
"`< 1 year x treatment 1`", "`1-2 years x treatment 1`", "`2-3 years x treatment 1`",
"`3-4 years x treatment 1`", "`4-5 years x treatment 1`", "`5-6 years x treatment 1`",
"`6-7 years x treatment 1`", "`7-8 years x treatment 1`", "`8-9 years x treatment 1`",
"`9-10 years x treatment 1`", "`> 10 years x treatment 1`", "`< 1 year x treatment 2`",
"`1-2 years x treatment 2`", "`2-3 years x treatment 2`", "`3-4 years x treatment 2`",
"`4-5 years x treatment 2`", "`5-6 years x treatment 2`", "`6-7 years x treatment 2`",
"`7-8 years x treatment 2`", "`8-9 years x treatment 2`", "`9-10 years x treatment 2`",
"`> 10 years x treatment 2`"))
reg_results_holding_periods_coefficients <- structure(list(estimate = c(0.285600060160589, 0.217927116290694,
0.134701599627727, 0.0896591006174163, 0.0734158463083404, 0.0608466549826728,
0.0486097030045517, 0.0401007072295853, 0.0308951206105791, 0.0253381679953092,
0.021513615579265, 0.661133167454535, 0.431578140877729, 0.265876430935115,
0.183658670199874, 0.138233507075241, 0.124403086427076, 0.0981645433309161,
0.11044124019981, 0.088975622267333, 0.0870994132668995, 0.0793922440017489,
-0.358219106875326, -0.112964312762333, -0.214750913450474, -0.148208470796826,
-0.136634845269832, -0.10278907400666, -0.0861694805522434, -0.0780928197988248,
-0.0717341952873777, -0.0489804255538236, -0.0280664342536037
), sample_sizes = c("Control group", "Control group", "Control group",
"Control group", "Control group", "Control group", "Control group",
"Control group", "Control group", "Control group", "Control group",
"Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1",
"Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1", "Treatment 1",
"Treatment 1", "Treatment 2", "Treatment 2", "Treatment 2", "Treatment 2",
"Treatment 2", "Treatment 2", "Treatment 2", "Treatment 2", "Treatment 2",
"Treatment 2", "Treatment 2"), year = c("< 1", "1-2", "2-3",
"3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10", "> 10", "< 1",
"1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10",
"> 10", "< 1", "1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8",
"8-9", "9-10", "> 10"), num_of_obs = c(11148L, 5339L, 5351L,
4995L, 5001L, 4823L, 4427L, 3965L, 3357L, 3128L, 37108L, 1446L,
551L, 342L, 262L, 225L, 198L, 150L, 139L, 97L, 87L, 1003L, 59L,
111L, 311L, 455L, 598L, 559L, 513L, 412L, 340L, 287L, 1548L),
row = 1:33, lower_ci = c(0.276899313667802, 0.208715239159497,
0.12929418479355, 0.0855358716610185, 0.0703639637264808,
0.0581478912016657, 0.0461085497023501, 0.0376386484961451,
0.0285457550948043, 0.0230553955578293, 0.0210421131537736,
0.635920530584401, 0.399007766963874, 0.242890856713076,
0.162910489271237, 0.121801854572761, 0.107986009329363,
0.0837232007680086, 0.0960924603750846, 0.071918513411995,
0.0724287431302565, 0.0754971216683171, -0.43656918503493,
-0.164705377781974, -0.235877113010794, -0.16269415680847,
-0.145726963787727, -0.111130412801045, -0.0934266578936087,
-0.0864271623965783, -0.0796390193104697, -0.0559375450272804,
-0.0305204141137063), upper_ci = c(0.294300806653376, 0.22713899342189,
0.140109014461903, 0.0937823295738141, 0.0764677288902001,
0.06354541876368, 0.0511108563067534, 0.0425627659630255,
0.033244486126354, 0.0276209404327892, 0.0219851180047563,
0.686345804324669, 0.464148514791584, 0.288862005157155,
0.20440685112851, 0.15466515957772, 0.140820163524789, 0.112605885893824,
0.124790020024536, 0.106032731122671, 0.101770083403543,
0.0832873663351808, -0.279869028715723, -0.0612232477426919,
-0.193624713890155, -0.133722784785182, -0.127542726751936,
-0.0944477352122748, -0.0789123032108781, -0.0697584772010714,
-0.0638293712642856, -0.0420233060803668, -0.0256124543935011
)), class = "data.frame", row.names = c("`< 1 year`", "`1-2 years`",
"`2-3 years`", "`3-4 years`", "`4-5 years`", "`5-6 years`", "`6-7 years`",
"`7-8 years`", "`8-9 years`", "`9-10 years`", "`> 10 years`",
"`< 1 year x treatment 1`", "`1-2 years x treatment 1`", "`2-3 years x treatment 1`",
"`3-4 years x treatment 1`", "`4-5 years x treatment 1`", "`5-6 years x treatment 1`",
"`6-7 years x treatment 1`", "`7-8 years x treatment 1`", "`8-9 years x treatment 1`",
"`9-10 years x treatment 1`", "`> 10 years x treatment 1`", "`< 1 year x treatment 2`",
"`1-2 years x treatment 2`", "`2-3 years x treatment 2`", "`3-4 years x treatment 2`",
"`4-5 years x treatment 2`", "`5-6 years x treatment 2`", "`6-7 years x treatment 2`",
"`7-8 years x treatment 2`", "`8-9 years x treatment 2`", "`9-10 years x treatment 2`",
"`> 10 years x treatment 2`"))
### ggplot settings ###
theme_set(theme_bw())
colors_cg_treat_1_treat_2_type_2 <- c("#9E9E9E", "steel blue", "#B71C1C") # alternatives: 1) dark grey 3) maroon
# making ggplot and saving it
p <- reg_results_holding_periods_coefficients %>% ggplot(aes(x=reorder(year, row), y=estimate, group=sample_sizes, fill=sample_sizes)) + # use reorder, otherwise ggplot will reorder the x-axis such that after "< 1 year", "> 10 years" will be depicted
geom_bar(position="dodge", stat="identity", width=0.825) +
geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), position=position_dodge(0.825), width = 0.2) +
theme(legend.position="bottom", legend.margin=margin(0,0,0,0), legend.box.margin=margin(-5,0,0,0),
legend.title = element_blank(),axis.title.x = element_text(size=12), # here u can change size of the x-axis title
axis.title.y = element_text(size=12), # here u can change size of the y-axis title
axis.text.x = element_blank()) +
scale_fill_manual(labels = c("Control group", "Treatment 1", "Treatment 2"), values = colors_cg_treat_1_treat_2_type_2) +
labs(title = "Results", x = "Years", y = "Dosage", color = "")
# remove lower_ci and upper_ci columns from reg_pivoted
reg_results_holding_periods_coefficients <- reg_results_holding_periods_coefficients %>% select(-lower_ci, -upper_ci,-row)
# rename column number of obs to n in reg_results_holding_periods_coefficients
reg_results_holding_periods_coefficients <- reg_results_holding_periods_coefficients %>% rename("n" = "num_of_obs")
# pivot wider the reg_results_holding_periods_coefficients based on year column
p1 <- reg_pivoted_1 <- reg_results_holding_periods_coefficients %>% pivot_wider(id_cols= c("sample_sizes"),id_expand = TRUE,
names_from = year, values_from = c("estimate"),names_sort = F,names_expand = F,
names_prefix = " ", names_sep = " ",
values_fill = list(n = 0, estimate = 0))
p2 <- reg_pivoted_2 <- reg_results_holding_periods_coefficients %>% pivot_wider(id_cols= c("sample_sizes"),id_expand = TRUE,
names_from = year, values_from = c("n"),names_sort = F,names_expand = F,
names_prefix = " ", names_sep = " ",
values_fill = list(n = 0, estimate = 0))
# full join p1 and p2
reg_pivoted <- full_join(p1,p2,keep =F)
## Joining with `by = join_by(sample_sizes, ` < 1`, ` 1-2`, ` 2-3`, ` 3-4`, `
## 4-5`, ` 5-6`, ` 6-7`, ` 7-8`, ` 8-9`, ` 9-10`, ` > 10`)`
# arrange the reg_pivoted based on sample_sizes
reg_pivoted <- reg_pivoted %>% arrange(sample_sizes)
# rename the column sample size to make identicial to document. If we tried to use n it gave us error that row names are not unique
reg_pivoted$sample_sizes <- c("Control group","n", "Treatment 1", "n1","Treatment 2","n2")
# rename sample_sizes to Groups
reg_pivoted <- reg_pivoted %>% rename("Groups" = "sample_sizes")
# create a gt table and reduce the decimal places to 2 for treatment rows and 0 for n rows
gt_table <- reg_pivoted %>%
gt(rowname_col = "sample_sizes") %>%
fmt_number(rows= c(2,4,6),columns = c(2:12), decimals = 0)
gt_table <- reg_pivoted %>%
gt(rowname_col = "sample_sizes") %>%
fmt_number(rows= c(1,3,5),columns = c(2:12), decimals = 3) %>%
tab_options(table.width= px(100),table.font.style ="normal", stub.border.width = px(2),
stub.border.color = "black",
) %>%
tab_style(
style = cell_borders(
sides = "all",
style = "solid"
),
locations = cells_body(
columns = everything(),
rows = everything() )) %>%
cols_align(
align = "center",
columns = everything()
)
gt_table <- gt_table %>% tab_style(
style = cell_borders(
sides = "all",
style = "solid"
),
locations= cells_column_spanners())
# theme=classic changes the border style of the table
# I am using this package basically https://rpkgs.datanovia.com/ggpubr/reference/ggtexttable.html .
# below line makes the n italics and font to Times New Roman
text_table <- ggtexttable(as.data.frame(gt_table),rows=NULL,
theme = ttheme("classic",base_size = 12,padding = unit(c(15, 4), "mm"),tbody_style(family="Times New Roman")))
# in the line below we are making the n row italics
text_table <- text_table %>% table_cell_font(row = c(3,5,7),column=c(1:12), face = "italic")
# this design variable should be written like this rather writing in 1 line
design <- "
#11
#2#
"
# by adjusting the numbers in heights and widths in below line u can make the table bigger or smaller and also adjust the margins. Spacing between table and legend can be adjusted by changing the heights parameter.
p/text_table+plot_layout(design=design,ncol=1,nrow=2,heights = c(3, 0.85),widths=c(1,6.5))

####=============================================================================================================####
# till above line your figure will be ready. Below are some extra options to get the similar figure with other packages or by saving ggplot as figure.
# but it will be of low quality since I am joining an image of ggplot to the table title.
p+annotation_custom(ggplotGrob(text_table),
xmin = 5.3, ymin = -2.5,
xmax = 5.7)

image <- ggplot_image(p, height = px(400), aspect_ratio = 4)
gt_table %>% tab_header(title = "",subtitle = md(image))
| Groups |
< 1 |
1-2 |
2-3 |
3-4 |
4-5 |
5-6 |
6-7 |
7-8 |
8-9 |
9-10 |
> 10 |
| Control group |
0.286 |
0.218 |
0.135 |
0.090 |
0.073 |
0.061 |
0.049 |
0.040 |
0.031 |
0.025 |
0.022 |
| n |
11148 |
5339 |
5351 |
4995 |
5001 |
4823 |
4427 |
3965 |
3357 |
3128 |
37108 |
| Treatment 1 |
0.661 |
0.432 |
0.266 |
0.184 |
0.138 |
0.124 |
0.098 |
0.110 |
0.089 |
0.087 |
0.079 |
| n1 |
1446 |
551 |
342 |
262 |
225 |
198 |
150 |
139 |
97 |
87 |
1003 |
| Treatment 2 |
−0.358 |
−0.113 |
−0.215 |
−0.148 |
−0.137 |
−0.103 |
−0.086 |
−0.078 |
−0.072 |
−0.049 |
−0.028 |
| n2 |
59 |
111 |
311 |
455 |
598 |
559 |
513 |
412 |
340 |
287 |
1548 |
grid.arrange(p, text_table, heights = c(5, 1.1),ncol=1,nrow=2)

cowplot::plot_grid(p, text_table, align = "hv", nrow = 2, axis="b",rel_heights = c(1.3, 0.3),ncol=1)
