有時(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的底層功底了,颯!