我有一個小的估計資料集,該資料集來自結果變數對主要治療變數的多次回歸,然后是各種控制變數集(實際上,這些控制變數的所有可能組合)。估算表如下:
df <-
structure(list(control_set = c("cen21_hindu_pct", "cen83_urban_pct",
"cen21_hindu_pct cen83_urban_pct", "NONE"), xest = c(0.0124513609978549,
0.00427174623249021, 0.006447506098051, 0.0137107176362076),
xest_conf_low = c(0.00750677700140716, -0.00436301983024899,
-0.0013089334064237, 0.00925185534519074), xest_conf_high = c(0.0173959449943027,
0.0129065122952294, 0.0142039456025257, 0.0181695799272245
)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))
我想為經典的“規格曲線分析”制作兩個圖。最上面的圖只是按主要處理變數的估計值排序的一組估計值(這里沒有問題):
df %>%
arrange(xest) %>%
mutate(specifications = 1:nrow(.)) %>%
ggplot(aes(x = specifications, y = xest, ymin = xest_conf_low, ymax = xest_conf_high))
geom_pointrange(alpha = 0.1, size = 0.6, fatten = 1)
labs(x = "", y = "Estimate\n")
theme_bw()
我的問題是下面的對齊圖描述了控制元件集的選擇。就在剛剛制作的圖中的每個系數點和須線的正下方,我想要一個指示該模型中包含的相應控制變數集的圖(即資料框行中control_set列中的控制串列)。df所以我在這個例子中需要的情節看起來像這樣:

這是我試圖到達那里的(失敗的)草圖,通過以長格式修改早期的估計資料集,但我無法垂直顯示多個刻度:(注意,這段代碼不會運行)
# forplot %>%
# arrange(xest) %>%
# mutate(specifications = 1:nrow(.)) %>%
# mutate(value = "|") %>%
# ggplot(aes(specifications, term))
# geom_text(aes(label = value))
# scale_color_manual(values = c("lightblue"))
# labs(x = "\nSpecification number", y = "")
# theme_bw()
如何使用 ggplot2 根據資料框中的資訊制作上面顯示的繪圖圖df?
uj5u.com熱心網友回復:
如果我們將您的情節定義為-> a...
library(patchwork)
b <- tibble(specifications = c(1,2,2,3),
control_set = rep(c("cen83_urban_pct", "cen21_hindu_pct"), each = 2)) %>%
ggplot(aes(specifications, control_set))
geom_text(aes(label = "|"), size = 5)
coord_cartesian(xlim = c(1,4))
labs(x = NULL, y = NULL)
theme_bw()
theme(axis.ticks = element_blank(),
axis.text.x = element_blank())
a/b plot_layout(heights = c(3,1))

如果你想自動生成密鑰,你可能會使用這樣的東西:
library(dplyr)
df %>%
select(control_set) %>%
mutate(specifications = 1:4) %>%
separate_rows(control_set, sep = "\\ ") %>%
mutate(control_set = trimws(control_set)) %>% # b/c my regex not good enough to trim spaces in line above
...
uj5u.com熱心網友回復:
如果您想使用 control_set 標簽重新標記 y 軸中的數字,您可以添加
scale_y_continuous(breaks = df$xest, labels = df$control_set)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416808.html
標籤:
