在多面图ggplot2上添加x轴标签

时间:2018-12-13 18:12:02

标签: r ggplot2 data-visualization facet

我有一个带有的多面geom_col()图,我希望在图的每个级别上都有x轴标签。所以在每一行之后,我都会有标签。我的图形当前如下所示:enter image description here

dput(res2)
structure(list(X = structure(c(8L, 1L, 7L, 9L, 6L, 4L, 5L, 3L, 
2L, 1L, 7L, 9L, 6L, 4L, 5L, 3L, 2L, 8L, 7L, 9L, 6L, 4L, 5L, 3L, 
2L, 8L, 1L, 9L, 6L, 4L, 5L, 3L, 2L, 8L, 1L, 7L, 6L, 4L, 5L, 3L, 
2L, 8L, 1L, 7L, 9L, 4L, 5L, 3L, 2L, 8L, 1L, 7L, 9L, 6L, 5L, 3L, 
2L, 8L, 1L, 7L, 9L, 6L, 4L, 3L, 2L, 8L, 1L, 7L, 9L, 6L, 4L, 5L
), .Label = c("Blue", "Green", "Magenta", "Maroon", "Orange", 
"Pink", "Purple", "Red", "Yellow"), class = "factor"), Phenotype = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Blue", "Green", "Magenta", 
"Maroon", "Orange", "Pink", "Purple", "Red", "Yellow"), class = "factor"), 
    heritability = c(0.12, 0.14, 0.34, 0.21, 0.33, 0.35, 0.25, 
    0.49, 0.19, 0.42, -0.12, 0.4, 0.13, 0.42, 0.47, 0.2, 0.17, 
    0.14, -0.1, 0.14, 0.45, 0.24, 0.47, -0.28, 0.34, 0.18, 0.15, 
    0.37, -0.47, 0.12, 0.17, -0.11, 0.53, 0.41, -0.2, 0.14, 0.26, 
    0.45, 0.41, 0.48, 0.15, -0.35, 0.22, 0.32, 0.29, 0.47, 0.17, 
    -0.25, 0.27, 0.38, 0.52, -0.11, 0.5, 0.28, 0.34, 0.31, 0.52, 
    0.14, -0.23, 0.21, 0.11, -0.42, 0.39, 0.32, 0.51, 0.39, 0.15, 
    0.46, 0.5, 0.42, 0.46, 0.18), pvalue = c(0.05, 0.09, 0.05, 
    0.05, 0.09, 0.02, 0.01, 0.1, 0.05, 0.04, 0.08, 0.01, 0.08, 
    0.05, 0.07, 0.06, 0.01, 0.04, 0.04, 0.01, 0.06, 0.1, 0.07, 
    0.01, 0.05, 0.02, 0.08, 0.1, 0.03, 0.06, 0.02, 0.08, 0.09, 
    0.01, 0.06, 0.04, 0.07, 0.03, 0.03, 0.07, 0.01, 0.01, 0.06, 
    0.05, 0.04, 0.06, 0.04, 0.03, 0.04, 0.04, 0.09, 0.1, 0.07, 
    0.01, 0.08, 0.06, 0.01, 0.07, 0.06, 0.08, 0.09, 0.1, 0.09, 
    0.01, 0.07, 0.05, 0.07, 0.06, 0.1, 0.1, 0.08, 0.09)), class = "data.frame", 
    row.names = c(NA, -72L))

这是我的情节代码:

A <- ggplot(res2, aes(Phenotype, heritability)) 
# uses a bar chart, geom_col represents hereditity values as the hights of the bars.  
A + geom_col(position = 'stack', fill = "#0000ff") +
  # Facets the data according to the Phenotypes in the X column of the data 
  facet_wrap(.~ X) +
  # Theme info: tilts the x-axis labels 90 degrees and pushes labels to be centered below the bars
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .4), plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5))+
  labs(title ="Heritability of Phenotype Permutations", subtitle = "P-values indicated")+
  # adds the pvalues above the bars, sets their position to be above or below the bar. 
  geom_text(aes(y = heritability + .06 * sign(heritability), label = pvalue), position = position_dodge(width = 0.9), size = 3.3)

1 个答案:

答案 0 :(得分:2)

facet_wrap中,您可以指定scales = 'free_x',这将允许每个子图具有自己的x轴,包括自己的轴标签。 (类似于free_yboth

在您的示例中,使用facet_wrap(.~X, scales= 'free_x')将产生以下曲线:

enter image description here