ggplot2

ggplot2 element_text()的warning

2021-03-25  本文已影响0人  一只烟酒僧

当我们使用element_text的color参数时,默认只能传一个颜色,但是我们有时会想给不同的标签上不同的颜色,所以会传一个向量,如element_text(color=1:10),这时候会报如下warning:

Warning message:
Vectorized input to `element_text()` is not officially supported.
Results may be unexpected or may change in future versions of ggplot2. 

这个说明其官方是不支持这种向量化操作的,其最终上色的结果是根据标签所处的位置,而不是标签本身,如,我原来x轴text为 a,c,d,b ,我想给ac上红色,bd上蓝色,因此会设置 element_text(color=c("red,"red","blue","blue")),但是由于ggplot本身会对轴label进行排序(如果是factor,会按level排,如果是character 会按照alpha-beta排),最终看到的上色结果是ab上红色,cd上蓝色

示例代码如下:

a<-data.frame(name=c("a","c","b","d"),
              value=c(1:4))
color<-ifelse(a$name%in%c("a","c"),"red","blue")
ggplot(a,aes(x=name,y=value))+geom_point()+
  theme(axis.text.x = element_text(color = color,size = 30))
image.png
上一篇下一篇

猜你喜欢

热点阅读