文章目录
- 刻度标签倾斜
- 替换x轴刻度标签
- 改变X刻度标签大小及颜色
演示数据集
library(ggplot2)
# 示例数据
data <- data.frame(
x = 1:5,
y = c(3, 5, 2, 7, 4)
)
data
# x y
#1 1 3
#2 2 5
#3 3 2
#4 4 7
#5 5 4
刻度标签倾斜
p1 <- ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = "identity")+
xlab("")+ # 取消x轴标签
ylab("") # 取消y轴标签
p2 <- ggplot(data, aes(x = x, y = y)) +
theme(axis.text.x = element_text(angle = -45))+ # 修改x轴刻度标签的倾斜角度
geom_bar(stat = "identity")+
xlab("")+ # 取消x轴标签
ylab("") # 取消y轴标签
p1+p2
替换x轴刻度标签
# 替换x轴刻度标签
p1 <- ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = "identity")+
xlab("")+ # 取消x轴标签
ylab("") # 取消y轴标签
# x轴的数据是连续型数据
p1 + scale_x_continuous(breaks = c(1, 2, 3, 4, 5),
labels = c("标签1", "标签2", "标签3", "标签4", "标签5"))
# x轴的数据是离散型数据
p1 + scale_x_discrete(labels = c("标签1", "标签2", "标签3", "标签4", "标签5"))
改变X刻度标签大小及颜色
p1 <- ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = "identity")+
xlab("")+ # 取消x轴标签
ylab("") # 取消y轴标签
p2 <- ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = "identity")+
theme(axis.text.x = element_text(color = "red",size = 12))+
xlab("")+ # 取消x轴标签
ylab("") # 取消y轴标签
p1+p2