R ggplot在x轴上的分组

R ggplot在x轴上的分组,r,ggplot2,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 %>%

我想复制这个图表,

尽管最低工资为欧元和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
图书馆(欧盟统计局)
图书馆(tidyverse)
图书馆(GG2)

dat_MW您可以使用
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")

你的问题没有简单的答案。这里有一个你可以考虑去适应的方法:把代码> > FAXETHOLD(~组)到你的情节会给你一些类似的东西。如果要复制原始绘图的外观,可以在
theme()
调用中使用镶嵌面标签的位置。