采用以下数据集,我谨树立以下形象。 我尝试了以下法典,但又回到了一个甚至不接近我所需要的时间段的档次。
我的数据
structure(list(UWLE = structure(c(0.600000023841858, 0.600000023841858,
0.800000011920929, 0.699999988079071, 0.899999976158142, 0.300000011920929,
0.400000005960464, 0.400000005960464, 0.400000005960464, 0.5,
0.400000005960464, 0.400000005960464, 0.5, 0.5, 0.600000023841858,
0.699999988079071, 0.699999988079071, 0.899999976158142, 0.800000011920929,
1), format.stata = "%9.0g"), HWLE = structure(c(10.1999998092651,
9.89999961853027, 9.39999961853027, 10.8000001907349, 12.3999996185303,
5, 4.90000009536743, 4.5, 5.30000019073486, 6.5, 7.59999990463257,
7.40000009536743, 7, 8.10000038146973, 9.5, 10.5, 10.3000001907349,
9.69999980926514, 11.1000003814697, 12.8999996185303), format.stata = "%9.0g"),
OWLE = structure(c(10.1999998092651, 10, 10, 9.89999961853027,
10.8999996185303, 9.10000038146973, 8.89999961853027, 8.80000019073486,
9, 10.1999998092651, 11.5, 11.1999998092651, 11.3000001907349,
11.3000001907349, 12.3999996185303, 9.89999961853027, 9.80000019073486,
9.80000019073486, 9.69999980926514, 10.6000003814697), format.stata = "%9.0g"),
OBLE = structure(c(10.1999998092651, 10.8999996185303, 10.3000001907349,
9.89999961853027, 9.10000038146973, 14.6000003814697, 15.3000001907349,
14.5, 14.3000001907349, 13.8000001907349, 13.1000003814697,
13.8000001907349, 13.1000003814697, 12.8000001907349, 12,
9.69999980926514, 10.3000001907349, 9.80000019073486, 9.39999961853027,
8.60000038146973), format.stata = "%9.0g"), TLE = structure(c(31.1000003814697,
31.3999996185303, 30.3999996185303, 31.2999992370605, 33.2000007629395,
29, 29.3999996185303, 28.2000007629395, 29, 31.1000003814697,
32.5999984741211, 32.9000015258789, 32, 32.7000007629395,
34.5, 30.8999996185303, 31.1000003814697, 30.1000003814697,
31, 33.0999984741211), format.stata = "%9.0g"), birth_place = structure(c(0,
1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4), format.stata = "%10.0g", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(Northeast = 0, Midwest = 1,
South = 2, West = 3, Foreign = 4)), race = structure(c(0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3), format.stata = "%18.0g", class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`non-Hispanic White` = 0,
`non-Hispanic Black` = 1, Hispanic = 2, `non-Hispanic Other` = 3
))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-20L))
Plot I需要上述数据
I. 审判
library(ggplot2)
# Convert labeled variables to factors
stack_data_G1$birth_place <- as.factor(stack_data_G1$birth_place)
stack_data_G1$race <- as.factor(stack_data_G1$race)
# Create the stacked horizontal bar plot
ggplot(stack_data_G1, aes(x = UWLE + HWLE + OWLE + OBLE, y = reorder(birth_place, -(UWLE + HWLE + OWLE + OBLE)), fill = race)) +
geom_col() +
coord_flip() +
labs(x = NULL, y = NULL, title = "XXXXXX", subtitle = "XXX", yaxis = "YYYY") +
scale_y_discrete(labels = c("UWLE", "HWLE", "OWLE", "OBLE")) +
theme_minimal() +
theme(
axis.text.y = element_text(size = 8),
axis.title.y = element_text(size = 10),
plot.title = element_text(size = 16),
plot.subtitle = element_text(size = 12),
panel.grid = element_blank()
)