x轴上的ggplot分组

Jus*_*kis 3 r ggplot2

我想复制这张图表 在此处输入图片说明

尽管以欧元和 PPS 为最低工资。该分组适用于以欧元为单位的值。一切正常,但我不明白如何在 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
Run Code Online (Sandbox Code Playgroud)

我感谢您的帮助 :)

nei*_*fws 5

您可以使用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")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明