ggplot在x轴上分组

时间:2017-06-02 03:09:10

标签: r ggplot2

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

尽管欧元和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
&#13;
&#13;
&#13;

感谢您的帮助:)。

1 个答案:

答案 0 :(得分:2)

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