ggplot2 X軸標(biāo)簽多重顏色

有時(shí)候我們希望用圖例來區(qū)分不用對(duì)象的屬性,但是如果想壓縮圖例數(shù)量,這時(shí)候可以考慮用坐標(biāo)標(biāo)簽文字的顏色來加以區(qū)分。這里給出一個(gè)笨辦法:用annotate來修改X軸。

圖是這樣的


代碼是這樣的:

library(tidyverse)
library(tidyverse)
library(ggplot2)
head(diamonds)
diamonds %>% mutate(YL=paste0("-RRR"),YY=0) -> mydf
ggplot()+geom_boxplot(data=mydf,mapping =aes(color,carat)) + 
 #theme(axis.text.x=element_text(hjust = 0,face="bold",size=10,angle=90,color=c("red")))+
  
  annotate(geom = "text", x = mydf$color, y = -0.25, label = mydf$YL, size = 3,color="blue",angle=90) + 
  annotate(geom = "text", x = unique(mydf$color), y = -0.45, label = unique(mydf$color), size = 3,color=c(rep("red ",4),rep("blue",3)),angle=90) + 
coord_cartesian(ylim = c(0,5), expand = FALSE, clip = "off") +
theme_bw() +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        legend.title=element_blank()
  )

不知道你有沒有更好的辦法呢?

theme(axis.text.x=element_text(hjust = -1,face="bold",size=10,angle=90,color=c("red","blue")))
  # geom_line(data= mydf,mapping = aes(YL,YY))
  #scale_x_reverse()

是時(shí)候動(dòng)用ggplot的底層功底了,颯!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容