ggplot在x轴上分组

3

我想复制这个图表,enter image description here

但是我想用欧元和购买力平价来表示最低工资。分组应用于欧元的值。一切运作良好,但我不知道如何在x轴上额外添加“分组”。

以下是我的代码:

library(eurostat)
library(tidyverse)
library(ggplot2)
dat_MW <- get_eurostat(id="earn_mw_cur", time_format="num")
dat_MW <- label_eurostat(dat_MW)
dat_MW_w <- dat_MW  %>% filter(time==2017, currency %in% c("Euro","Purchasing Power Standard")) %>% arrange(currency, values)
dat_MW_w$geo[dat_MW_w$geo=="Germany (until 1990 former territory of the FRG)"] <- "Germany"
dat_MW_w$geo[dat_MW_w$geo=="Former Yugoslav Republic of Macedonia, the"] <- "Macedonia"
dat_MW_w$currency[dat_MW_w$currency=="Purchasing Power Standard"] <- "PPS"
dat_MW_w$currency[dat_MW_w$currency=="Euro"] <- "EUR"
dat_MW_w <- dat_MW_w %>% 
        mutate(group=ifelse(values<=500 & currency=="EUR","GROUP1", 
                            ifelse(values<=1000 & currency=="EUR", "GROUP2", 
                                   ifelse(currency=="EUR","GROUP3", NA))))
figure1 <- ggplot(data=dat_MW_w, aes(x=reorder(geo, values), y=values, group=currency)) +
        xlab("Countries") + ylab("EUR/PPS") +
        #ggtitle("Monthy minium wages, in EUR/PPS, 2017 S1") +
        geom_bar(aes(fill=currency),stat = "identity", position = position_dodge()) + 
        theme_minimal() + 
        scale_fill_manual(values=c("#999999", "#E69F00"))+
        theme(axis.text.x = element_text(angle = 90, hjust = 1))
figure1

非常感谢您的帮助 :)


你的问题没有简单的答案。这里提供一种可供考虑的方法:在ggplot线图中使用多行x轴标签 - Adam Quek
+ facet_wrap(~ group) 添加到您的图表中将会得到类似的效果。如果想要复制原始图表的外观,可以在 theme() 调用中尝试调整分面标签的位置。 - Marius
1个回答

7
您可以使用facet_grid来实现类似的效果:
ggplot(data=dat_MW_w, aes(x=reorder(geo, values), y=values, group=currency)) +
    xlab("Countries") + ylab("EUR/PPS") +
    #ggtitle("Monthy minium wages, in EUR/PPS, 2017 S1") +
    geom_bar(aes(fill=currency),stat = "identity", position = position_dodge()) + 
    theme_minimal() + 
    scale_fill_manual(values=c("#999999", "#E69F00"))+
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
    facet_grid(.~group, scales = "free", switch = "x", space = "free_x") + 
    theme(strip.placement = "outside")

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接