3

I' would like to replicate this chart, enter image description here

though with minimum wages in EUR and PPS. The grouping applies to values in EUR. Everything works fine, but I don't get it how to add the "group" additionally on x axis. here my code

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

I appreciate your help :)

camille
  • 16,432
  • 18
  • 38
  • 60
Justas Mundeikis
  • 935
  • 1
  • 10
  • 19
  • There's no easy answer to your question. Here's an approach you may consider to adapt: [Multi-row x-axis labels in ggplot line chart](https://stackoverflow.com/questions/20571306/multi-row-x-axis-labels-in-ggplot-line-chart). – Adam Quek Jun 02 '17 at 03:36
  • addding `+ facet_wrap(~ group)` to your plot would get you something similar. If you want to replicate the look of the original plot you can play around with positions of the facet labels in the `theme()` call. – Marius Jun 02 '17 at 04:38

1 Answers1

7

You can get a similar effect using 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

neilfws
  • 32,751
  • 5
  • 50
  • 63