参考自《R Graphics Cookbook》
修改坐标轴上刻度标签的外观样式。
library(ggplot2)
#底图
pg_plot <- ggplot(PlantGrowth, aes(x = group, y = weight)) +
geom_boxplot() +
scale_x_discrete(
breaks = c("ctrl", "trt1", "trt2"),
labels = c("Control", "Treatment 1", "Treatment 2")
)
#查看底图
pg_plot
一、调整x轴上坐标文本为90度显示
pg_plot +
theme(axis.text.x = element_text(angle = 90))
#发现上图生成的坐标文本靠右了、靠下了,微调之
pg_plot +
theme(axis.text.x = element_text(angle = 90,
hjust = 1,
vjust = .5))
#调整为30度更加合适
pg_plot +
theme(axis.text.x = element_text(angle = 30,
hjust = 1,
vjust = 1))
二、调整字体、字型、字型、颜色,大小
pg_plot +
theme(
axis.text.x = element_text(family = "serif", face = "italic",
colour = "darkred", size = rel(2))
)