簡體   English   中英

ggplot2:在刻面圖上着色軸文本

[英]ggplot2: Coloring axis text on a faceted plot

scales參數設置為"free"時,我似乎無法在刻面圖上正確地着色軸文本。 請考慮以下數據集:

library( ggplot2 )
X <- data.frame( V1 = LETTERS, V2 = runif( 26 ),
    V3 = rep( c("F1", "F2"), each = 13 ) )

我們可以在單個面上繪制數據,突出顯示字母D,O,T,如下所示:

v <- ifelse( X$V1 %in% c( "D", "O", "T" ), "red", "black" )
g <- ggplot( X, aes( x = V1, y = V2 ) ) + geom_point() +
    theme( axis.text.x = element_text( color = v ) )

使用默認的scales = "fixed"正確地繪制切面,在兩個面上突出顯示D,O,T。

g + facet_wrap( ~V3 )

在此輸入圖像描述

但是,將scales參數切換為"free"會導致意外行為,其中只突出顯示D和Q.

g + facet_wrap( ~V3, scales = "free" )

在此輸入圖像描述

我的問題:這是一個錯誤還是我需要以某種方式修改我對v定義來解釋自由規模。 如果它是一個錯誤,是否有人知道一個解決方法,以突出顯示每個(自由縮放)方面的特定軸文本?


編輯:正如Henrik所建議的那樣,自己的答案轉移到了答案。

我不認為這是一個錯誤。 問題是這里的v基本上是一串字符,長度為26,它定義了x軸上前26個中斷的顏色。 當x軸完全有26次斷裂時,良好且良好; 當它小於那個(當你設置scales="free"時就是這種情況),它只是在每個軸的開頭重新開始。 Q在這里是紅色的,因為它位於第二個圖中的第四個位置,盡管在第一個圖中v[4]的紅色表示D。

根據我在SO上嘗試和閱讀的內容,我們無法將美學映射到theme() ,它控制ggplot中軸文本的外觀。

可以通過隱藏軸並使用geom_text()代替模擬軸來破解解決方案,因為后者確實接受從數據映射的美學。 但它可能不是很優雅:

g2 <- ggplot(cbind(X, v), #add v to X
             aes(x = V1, y = V2)) + 
  geom_point() +
  # make space to accommodate the fake axis
  expand_limits(y = -0.05) +
  # create a strip of white background under the fake axis
  geom_rect(ymin = -5, ymax = 0, xmin = 0, xmax = nrow(X) + 1, fill = "white") +
  # fake axis layer, aligned below y = 0
  geom_text(aes(colour = v, label = V1), y = 0, vjust = 1.1) +
  # specify the font colours for fake axis
  scale_colour_manual(values = c("black", "red"), guide = F) +
  # hide the actual x-axis text / ticks
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank())

g2 + facet_wrap( ~V3, scales = "free" )

帶假軸的刻面圖

在挖掘了與繪圖相關的圖形對象(grobs)之后,我遇到了一個潛在的黑客來解決這個問題。 雖然不如Z.Lin的解決方案那么優雅,但我想分享它用於教育目的。

我們首先檢索grobs

gt <- ggplotGrob( g + facet_wrap( ~V3, scales = "free" ) )
## TableGrob (11 x 11) "layout": 20 grobs
##     z         cells        name                                  grob
## 1   0 ( 1-11, 1-11)  background       rect[plot.background..rect.105]
## 2   1 ( 7- 7, 4- 4)   panel-1-1               gTree[panel-1.gTree.17]
## 3   1 ( 7- 7, 8- 8)   panel-2-1               gTree[panel-2.gTree.30]
## 4   3 ( 5- 5, 4- 4)  axis-t-1-1                        zeroGrob[NULL]
## 5   3 ( 5- 5, 8- 8)  axis-t-2-1                        zeroGrob[NULL]
## 6   3 ( 8- 8, 4- 4)  axis-b-1-1    absoluteGrob[GRID.absoluteGrob.43]
## 7   3 ( 8- 8, 8- 8)  axis-b-2-1    absoluteGrob[GRID.absoluteGrob.50]
## 8   3 ( 7- 7, 7- 7)  axis-l-1-2    absoluteGrob[GRID.absoluteGrob.64]
## 9   3 ( 7- 7, 3- 3)  axis-l-1-1    absoluteGrob[GRID.absoluteGrob.57]
## 10  3 ( 7- 7, 9- 9)  axis-r-1-2                        zeroGrob[NULL]
## 11  3 ( 7- 7, 5- 5)  axis-r-1-1                        zeroGrob[NULL]
## 12  2 ( 6- 6, 4- 4) strip-t-1-1                         gtable[strip]
## 13  2 ( 6- 6, 8- 8) strip-t-2-1                         gtable[strip]
## 14  4 ( 4- 4, 4- 8)      xlab-t                        zeroGrob[NULL]
## 15  5 ( 9- 9, 4- 8)      xlab-b titleGrob[axis.title.x..titleGrob.33]
## 16  6 ( 7- 7, 2- 2)      ylab-l titleGrob[axis.title.y..titleGrob.36]
## 17  7 ( 7- 7,10-10)      ylab-r                        zeroGrob[NULL]
## 18  8 ( 3- 3, 4- 8)    subtitle zeroGrob[plot.subtitle..zeroGrob.102]
## 19  9 ( 2- 2, 4- 8)       title    zeroGrob[plot.title..zeroGrob.101]
## 20 10 (10-10, 4- 8)     caption  zeroGrob[plot.caption..zeroGrob.103]

Grobs是分層對象,遍歷這些結構的一般規則分為兩類:

  1. 如果GROB是類型的gtable (如gt以上),訪問該進入表個體grobs可以通過進行$grobs
  2. 如果gtable不是gtable類型,它的子窗口可以通過$children訪問。

觀察上面的gtable ,我們觀察到凹槽6和7分別對應於刻面1和2的底軸。 每個軸grobs的類型是absoluteGrob ,所以使用上述兩個規則,我們可以檢查它們是由什么了的是這樣的:

gt$grobs[[6]]$children
## (zeroGrob[axis.line.x..zeroGrob.40], gtable[axis]) 
##  and likewise for gt$grobs[[7]]$children

注意到第二個孩子是gtable ,我們可以繼續降低grobs的層次結構,直到我們到達gt$grobs[[6]]$children[[2]]$grobs[[2]]$children[[1]] ,它是grob層次結構的一個葉子(它的$childrenNULL )並且對應於軸文本。 讓我們檢查一下它的圖形參數,可以通過$gp訪問它們:

## Double-check that we have the correct text object
gt$grobs[[6]]$children[[2]]$grobs[[2]]$children[[1]]$label
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"

## Display the summary of graphical parameters
str( gt$grobs[[6]]$children[[2]]$grobs[[2]]$children[[1]]$gp )
## List of 5
##  $ fontsize  : num 8.8
##  $ col       : chr [1:26] "black" "black" "black" "red" ...
##  $ fontfamily: chr ""
##  $ lineheight: num 0.9
##  $ font      : Named int 1
##   ..- attr(*, "names")= chr "plain"
##  - attr(*, "class")= chr "gpar"

請注意, col屬性的長度為26,並且與問題中的v變量完全對應。 如果我們查看第二個方面的底部軸( gt$grobs[[7]]$... ),我們會看到在那里使用相同的col值,導致兩個方面的相同軸文本着色(如在Z.Lin的解決方案中提出)。

因此,將這些顏色設置僅設置為v “手動”的相應部分允許我們修改原始圖並實現期望的結果。

gt$grobs[[6]]$children[[2]]$grobs[[2]]$children[[1]]$gp$col <- v[1:13]
gt$grobs[[7]]$children[[2]]$grobs[[2]]$children[[1]]$gp$col <- v[14:26]
grid::grid.draw( gt )

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM