Double-label x-axis

Hello guys,

I need help with labeling the x-axis of my barplot. I used "names.arg" for showing the years of the values, but I also want to show the names of the different barplots. I would need a second layer of "names.arg" to show the names, but I haven't found a function to do it.

quotenDE<-c(27.67097, 27.98674, 41.27499, 37.12439 ,44.32205, 46.22212, 43.76696 ,47.14954)
quotenDEMIGRA<-c(18.76790 ,20.40880 ,27.84180, 25.03935, 32.47452, 37.10825 ,34.26004 ,46.01934)
quotenAusland<-c(13.35735, 18.02211, 24.23222 ,21.30554, 23.74047, 23.48691, 27.96400, 38.82470)
jahre2<-c("2003", "2007", "2007" ,"2010", "2012", "2014", "2016" ,"2018")

##barplot
Migraquoten<-cbind(quotenDE, quotenDEMIGRA, quotenAusland)
migra<-as.matrix(Migraquoten)
JahreMigra<-cbind(jahre2, jahre2,jahre2)
MIGRA<-barplot(Migraquoten,
        beside=T,                                     
        names.arg=JahreMigra,
        col=c('lightblue', 'lightblue', "cyan4", "cyan4", "cyan4", "cyan4", "cyan4", "cyan4"),   
        ylim=c(0,60),                                
        xpd=FALSE,
        ylab="Beteiligung in %",
        las=2,
        main="Berufsbezogene Weiterbildung in Deutschland 2003-2018 nach Migrationshintergrund",
        cex.axis = 0.8, cex.names = 0.7)
text(MIGRA, Migraquoten-2, round(Migraquoten, 1), col = "black", cex=0.6)
legend("topleft", c("BSW", "AES"), pch=15, col=c("lightblue", "cyan4"),
       cex=1.3, bty = "n", y.intersp = 1)

Thank you in advance.

Because I can only add one picture in the post I have to make a reply.

I want to have it like this:

this is not a reproducible example as it based on private objects, therefore it is substantially difficult for forum users to run your code.

Therefore it is recommended that you use

dput(Migraquoten)

and

dput(JahreMigra)

and include these in your post.

1 Like

Hi Pumpernickel,

I've used a slightly different approach and use the facet_wrap()function to split the 3 groups of quoten in 3 plots.

However, it's important to note that I use the ggplot2 package, which is part of

library(tidyverse)
theme_set(theme_minimal())

migra_quoten <- tibble(quotenDE = c(27.67097, 27.98674, 41.27499, 37.12439 ,44.32205, 46.22212, 43.76696 ,47.14954),
                       quotenDEMIGRA = c(18.76790 ,20.40880 ,27.84180, 25.03935, 32.47452, 37.10825 ,34.26004 ,46.01934),
                       quotenAusland = c(13.35735, 18.02211, 24.23222 ,21.30554, 23.74047, 23.48691, 27.96400, 38.82470),
                       jahre2 = c("2003", "2005", "2007" ,"2010", "2012", "2014", "2016" ,"2018"),
                       )

df <- migra_quoten %>% 
  pivot_longer(starts_with("quoten"), names_to = "quoten", values_to = "beteiligung") %>%
  ## change the `quoten` values from shortname to more descriptive values  
  mutate(quoten = case_when(quoten == "quotenDE" ~ "Deutsch ohne Migrationshintergrund",
                            quoten == "quotenDEMIGRA" ~ "Deutsch mit Migrationshintergrund",
                            TRUE ~ "Ausländer"
                            ) %>% 
           ## this changes the order of the plots: 
           ## with `fct_inorder` it's dependant on the ordering of the rows
           fct_inorder()
         )
  
df %>% 
  ggplot(aes(jahre2, beteiligung, fill = beteiligung)) +
  geom_col() +
  facet_wrap(~ quoten) +
  labs(title = "Berufsbezogene Weiterbildung in Deutschland 2003-2018 nach Migrationshintergrund",
       x = "",
       y = "Beteiligung in %"
       ) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 1),
        legend.position = "none"
        )

Created on 2021-02-15 by the reprex package (v1.0.0)

First of all, I changed the dataset class from matrix to a data.frame and made sure (with pivot_longer()) that every observation is on a separate row.
Then, the mutate() function is used to rename the 'quoten shortnames' and change the column into a factor type.

The coloring of the plots with fill= is controlled from the beteiliging column, but if you add an extra column for the BSW and AES values and modify the line legend.position= value, you should be able to finalize it.

I hope this helps you forward.

1 Like

Thank you very much!! I really appreciate your effort to help me.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.