如何调整ggplot组合图的图例条目顺序与样式以匹配原图?
求助:复现图表时的图例调整问题
我想要复现一张不含葡萄牙高亮的图表(原图为柱状图搭配散点,区分「显著」「不显著」两类数据,同时对比「调整阅读表现前」和「调整阅读表现后」两组),数据已导入并转换为长格式,但卡在了图例条目顺序调整上,当前生成的图例和原图差异较大,恳请协助解决。
附上我的代码:
# load data f5_5_data_before <- read_excel("uz49al.xlsx", sheet = "Figure1.20", range = "A32:E68") names(f5_5_data_before)[1] <- "Country" names(f5_5_data_before)[2] <- "Odds_ratio" names(f5_5_data_before)[3] <- "SE" names(f5_5_data_before)[4] <- "sig" names(f5_5_data_before)[5] <- "non_sig" f5_5_data_before$Country <- as.factor(f5_5_data_before$Country) f5_5_data_before <- f5_5_data_before %>% mutate( category = case_when( is.na(sig) ~ "Non-significant", !is.na(sig) ~ "Significant" ), value = case_when( category == "Non-significant" ~ non_sig, category == "Significant" ~ sig ) ) f5_5_data_before$group2 <- "Before accounting for reading performance" f5_5_data_after <- read_excel("uz49al.xlsx", sheet = "Figure1.20", range = "A32:I68") f5_5_data_after <- f5_5_data_after[, c(1, 6:9)] names(f5_5_data_after)[1] <- "Country" names(f5_5_data_after)[2] <- "Odds_ratio" names(f5_5_data_after)[3] <- "SE" names(f5_5_data_after)[4] <- "sig" names(f5_5_data_after)[5] <- "non_sig" f5_5_data_after$Country <- as.factor(f5_5_data_after$Country) f5_5_data_after <- f5_5_data_after %>% mutate( category = case_when( is.na(sig) ~ "Non-significant", !is.na(sig) ~ "Significant" ), value = case_when( category == "Non-significant" ~ non_sig, category == "Significant" ~ sig ) ) f5_5_data_after$group2 <- "After accounting for reading performance" # appending in long format f5_5_data <- rbind(f5_5_data_after, f5_5_data_before) # shaded rectangle rect1 <- data.frame( xmin = 14.5, xmax = 15.5, ymin = -Inf, ymax = Inf ) # figure f5_5 <- ggplot() + geom_col(data = f5_5_data %>% filter(group2 == "After accounting for reading performance"), aes(x = reorder(Country,-Odds_ratio), y = value, fill = category, colour = group2), width=0.5, ) + geom_point( data = f5_5_data %>% filter(group2 == "Before accounting for reading performance"), aes(x = Country, y = value, fill = category, colour = group2), shape = 23, size = 3, ) + geom_rect( data = rect1, aes( xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax ), alpha = 0.5, inherit.aes = FALSE ) + scale_y_continuous(breaks = pretty_breaks(), limits = c(0, 25), expand = c(0, 0)) + labs(x = NULL, y = "Odds ratio") + theme(axis.text.x = element_text(angle = 90)) print(f5_5)
当前输出的图表中,图例条目顺序和样式与原图差异明显,无法匹配目标效果,急需解决这一问题。
内容的提问来源于stack exchange,提问作者samuelsson91
相关产品推荐
相关产品推荐

