ggplot2:在分面图上着色轴文本 [英] ggplot2: Coloring axis text on a faceted plot

查看:151
本文介绍了ggplot2:在分面图上着色轴文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比例参数设置为free。考虑以下数据集:

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

我们可以在单个方面绘制数据,如下所示突出显示字母D,O,T:

 <$ (X,V1,%,C,D,O,T),红,黑)
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)

然而,切换尺度

code>参数到free导致意想不到的行为,只有D和Q被突出显示。

  g + facet_wrap(〜V3,scales =free )



我的问题:这是一个错误,还是我需要以某种方式修改 v 的定义来说明自由比例。如果这是一个错误,是否有人知道解决方法,以突出每个(免费缩放)方面的特定轴文本?




<编辑:自己的答案转移到答案,正如Henrik所建议的那样。

解决方案

我不认为这是一个错误。问题是这里的 v 基本上是一串字符,长度为26,它定义了x轴上前26个断点的颜色。当x轴准确地具有26个中断时,良好&好;当它小于这个值时(这是你设置 scales =free)的情况),它只是在每个轴的开始处重新开始。 Q在这里是红色的,因为它在第二个图中位于第四个位置,尽管在第一个图中 v [4] 的红色是用于D的。



根据我试过的&在这里阅读,不能将美学映射到控制ggplot中轴文本外观的 theme()



可以通过隐藏轴&使用 geom_text()代替模拟轴,因为后者确实接受从数据映射的美学。虽然它可能不是很优雅:

  g2 < -  ggplot(cbind(X,v),#add v to X 
aes(x = V1,y = V2))+
geom_point()+
#使空间容纳假轴
expand_limits(y = -0.05)+
#在假轴下创建一个白色背景条带
geom_rect(ymin = -5,ymax = 0,xmin = 0,xmax = nrow(X)+1,fill =white)+
#假轴图层,在y = 0之下对齐
geom_text(aes(color = v,label = V1),y = 0,vjust = 1.1)+
#指定伪造的字体颜色axis
scale_colour_manual(values = c(black,red),guide = F)+
#隐藏实际的x轴文本/滴答
theme(axis.text.x = element_blank(),axis.ticks.x = element_blank())

g2 + facet_wrap(〜V3,scales =free)


I seem unable to correctly color axis text on a faceted plot when the scales parameter is set to "free". Consider the following dataset:

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

We can plot the data on a single facet, highlighting the letters D, O, T as follows:

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 ) )

Making the plot faceted using the default scales = "fixed" correctly highlights D, O, T on both facets.

g + facet_wrap( ~V3 )

However, switching the scales parameter to "free" leads to unexpected behavior, where only D and Q are highlighted.

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

My question: is this a bug or do I need to somehow modify my definition of v to account for free scales. If it is a bug, does anybody know of a workaround to highlight specific axis text in each (free-scaled) facet?


EDIT: Own answer moved to answers, as suggested by Henrik.

解决方案

I don't think it's a bug. The problem is that v here is basically a string of characters, length 26, which defines colours for the first 26 breaks on the x-axis. When the x-axis has 26 breaks exactly, well & good; when it has less than that (which is the case when you set scales="free"), it simply restarts at the beginning for each axis. Q is red here because it's in the fourth position in the second plot, although the v[4]'s red was meant for D, in the first plot.

Based on what I've tried & read here on SO, one can't map aesthetics into theme(), which controls the appearance of axis text in ggplot.

It's possible to hack a solution by hiding the axis & using geom_text() instead to simulate an axis, since the latter does accept aesthetics mapped from the data. It may not be very elegant, though:

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" )

这篇关于ggplot2:在分面图上着色轴文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆