小技巧9-ggpolt 横坐标文本倾斜角度调整

ggplot的横坐标轴标签可以倾斜,不做赘述:

axis.text.x = element_text(angle = 45,vjust = 0.5,hjust = 0.5)

但是!!不好看!45°的倾斜的角度有点过头,位置上text和sticks离得太远。

就像这样,不好看

经过调整和摸索,倾斜角度和位置调成这样更好看些:

axis.text.x = element_text(angle = 30,vjust = 0.85,hjust = 0.75)
像这样,好看

完整代码如下:

library(ggplot2)

p1 <- ggplot(data1_SE,aes(x = addin,y = Ratio))+
  geom_bar(stat="identity",width = 0.6,fill = "red")+
  geom_errorbar(data = data1_SE,aes(ymin = Ratio - se, ymax = Ratio + se),width = 0.2,size = 1.5)+
  geom_point(data = data1,aes(x = addin,y = Ratio),stat="identity",size = 3,alpha=0.5)+
  theme(panel.background = element_blank(),
        panel.grid = element_blank(), 
        axis.line = element_line(colour = "#000000",size = 2),
        axis.text = element_text(colour = "#000000" ,size = 27),
        axis.text.x = element_text(angle = 30,vjust = 0.85,hjust = 0.75), ##就是这里
        axis.ticks = element_line(colour = "#000000" ,size = 2) ,
        axis.ticks.length = unit(2,'mm'),
        plot.margin = unit(c(0.5,0,0,0),"cm"),
        axis.title.y = element_text(size = 27),
        axis.title.x = element_blank(),
        plot.title = element_text(hjust = 0.5),
        legend.position = "none")+
  scale_y_continuous(limits = c(0,100),breaks = seq(0,100,20),expand = c(0, 0))+
  scale_x_discrete(labels = c("1.34e+7","6.70e+7","3.35e+8"))+
  ylab("Ratio(%)")

p1

——这一part不算干货,主要为自己方便查询

你可能感兴趣的:(小技巧9-ggpolt 横坐标文本倾斜角度调整)