简体   繁体   English

双重标记 x 轴

[英]Double labeling the x-axis

I need help with labeling the x-axis of my barplot.我需要帮助标记我的条形图的 x 轴。 I used "names.arg" for showing the years of the values, but I also want to show the names of the different barplots.我使用“names.arg”来显示值的年份,但我也想显示不同条形图的名称。 I would need a second layer of "names.arg" to show the names, but I haven't found a function to do it.我需要第二层“names.arg”来显示名称,但我还没有找到 function 来做到这一点。

jahre2<-c("2003", "2007", "2007" ,"2010", "2012", "2014", "2016" ,"2018")
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)
##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)

What it looks like now:现在的样子:

在此处输入图像描述

What it should look like:它应该是什么样子:

在此处输入图像描述

Since this question has been tagged with the ggplot2 tag, I'll answer with a solution in ggplot.由于此问题已使用ggplot2标记进行标记,因此我将在 ggplot 中提供解决方案。 What we're going to do to get the appearance you want, is use facetting and displaying the strips underneath the x-axis.为了获得您想要的外观,我们要做的是使用刻面并在 x 轴下方显示条带。

library(ggplot2)
library(scales)

# Data as by post author
jahre2<-c("2003", "2007", "2007" ,"2010", "2012", "2014", "2016" ,"2018")
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)

# Collect data in data.frame
df <- data.frame(
  jahre = jahre2,
  label = factor(c("BSW", "BSW", rep("AES", 6)), levels = c("BSW", "AES")), # inferred
  DE = quotenDE,
  DEMIGRA = quotenDEMIGRA,
  Ausland = quotenAusland
)

# Reshape data to wide format and set factor levels
df <- tidyr::pivot_longer(df, c(-jahre, -label))
df$name <- factor(df$name, levels = c("DE", "DEMIGRA", "Ausland"))

# We need a weird interaction term because 'jahre' has duplicates within a series
ggplot(df, aes(interaction(jahre, label), value)) +
  geom_col(aes(fill = label), colour = "black", width = 1) +
  geom_text(aes(label = number(value, 0.1)), 
            angle = 45, nudge_y = -2, size = 3) +
  # We set the labels to a function to strip out the 'label' part of the interaction
  scale_x_discrete(labels = function(x){substr(x, 1, 4)}, name = NULL,
                   expand = c(0,1)) +
  scale_y_continuous(limits = c(0, 60), expand = c(0,0),
                     name = "Beteiligung in %") +
  scale_fill_manual(values = c("lightblue", "cyan4"), name = NULL) +
  facet_grid(~ name, switch = "x", labeller = as_labeller(
    c("DE" = "Deutsch ohne Migrationshintergrund",
      "DEMIGRA" = "Deutsch mit Migrationshintergrund",
      "Ausland" = "Auslander")
  )) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        strip.placement = "outside", # <- strip under x-axis
        legend.position = c(0,1),
        legend.justification = c(0,1),
        axis.line.y = element_line(colour = "black"),
        axis.ticks.y = element_line(colour = "black"),
        panel.grid = element_blank(),
        axis.text = element_text(colour = "black"))

Created on 2021-02-15 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 2 月 15 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM