Art*_*lov 9 r ggplot2 facet-wrap
当scales参数设置为时,我似乎无法在刻面图上正确地着色轴文本"free".请考虑以下数据集:
library( ggplot2 )
X <- data.frame( V1 = LETTERS, V2 = runif( 26 ),
V3 = rep( c("F1", "F2"), each = 13 ) )
Run Code Online (Sandbox Code Playgroud)
我们可以在单个面上绘制数据,突出显示字母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 ) )
Run Code Online (Sandbox Code Playgroud)
使用默认值scales = "fixed"正确地绘制切面图会突出显示两个面上的D,O,T.
g + facet_wrap( ~V3 )
Run Code Online (Sandbox Code Playgroud)
但是,切换scales参数会"free"导致意外行为,其中仅突出显示D和Q.
g + facet_wrap( ~V3, scales = "free" )
Run Code Online (Sandbox Code Playgroud)
我的问题:这是一个错误还是我需要以某种方式修改我的定义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" )
Run Code Online (Sandbox Code Playgroud)
在探究了与情节相关的图形对象(毛刺)之后,我遇到了一个潜在的黑客来解决问题。尽管不像Z.Lin的解决方案那么优雅,但我还是出于教育目的共享它。
我们首先以
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]
Run Code Online (Sandbox Code Playgroud)
阻塞是分层对象,遍历这些结构的一般规则分为两类:
gtable(gt如上),则可以通过访问表中的单个grob $grobs。gtable杂项不是NOT类型,则可以通过访问其子杂项$children。纵观gtable上述,我们观察到grobs 6和7对应于刻面1和2,分别的底部轴。这些轴grob的类型都是absoluteGrob,因此使用上面的两个规则,我们可以像这样检查它们的组成:
gt$grobs[[6]]$children
## (zeroGrob[axis.line.x..zeroGrob.40], gtable[axis])
## and likewise for gt$grobs[[7]]$children
Run Code Online (Sandbox Code Playgroud)
注意第二个孩子是a gtable,我们可以继续下降grob的层次结构,直到到达gt$grobs[[6]]$children[[2]]$grobs[[2]]$children[[1]],这是grob层次结构的叶子(它$children是NULL)并且对应于轴文本。让我们检查其图形参数,可以通过$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"
Run Code Online (Sandbox Code Playgroud)
请注意,该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 )
Run Code Online (Sandbox Code Playgroud)