主题:axis
基本知识点
Element | Setter | Desceription |
---|---|---|
axis.line | element_line() | line parallel to axis (hidden in default themes) |
axis.text | element_text() | tick labels |
axis.text.x | element_text() | x-axis tick labels |
axis.text.y | element_text() | y-axis tick labels |
axis.title | element_text() | axis titles |
axis.title.x | element_text() | x-axis title |
axis.title.y | element_text() | y-axis title |
axis.ticks | element_line() | axis tick marks |
axis.ticks.length | unit() | length of tick marks |
实践绘图
element_text()
:文字属性,如color、size等。
element_line()
:线条属性,如linetype, size等。
df <- data.frame(x = 1:100, y = sample(c(1:100),100))
base <- ggplot(df, aes(x, y)) + geom_point()
base
base +
ylab("This is y axis title")+
xlab("This is x axis title")+
theme(
axis.text.x = element_text(color = "blue", size = 12),
axis.text.y = element_text(colour = "red", size = 16),
axis.title.x = element_text(face = "bold", colour = "red", size = 12),
axis.title.y = element_text(face = "italic", colour = "green", size = 16),
axis.line = element_line(colour = "blue", size = 4, linetype = 3),
axis.ticks.length = unit(0.4,"in"),
axis.ticks = element_line(colour = "purple", size = 2, linetype = 3)
)
x axis 角度(angle) 在科研绘图时经常用到,这里参考官方文件推荐设置方式,负角度
The most common adjustment is to rotate the x-axis labels to avoid long overlapping labels. If you do this, note negative angles tend to look best and you should set
hjust = 0 and vjust = 1
base +
theme(
axis.text.x = element_text(color = "blue", size = 12, angle = -45, hjust = 0, vjust = 1)
)
小结
1、设置的对象是字体,就是element_text()
2、设置的对象是线条,就是element_line()
3、注意tick label 和title的区别