目录:
- 初始图样
- 修改坐标轴的显示范围
- 修改坐标轴的标签(内容、大小、字体、颜色、加粗、位置、角度)
- 修改坐标轴刻度的文字(大小、字体、颜色、加粗、位置、角度)
- 修改坐标轴刻度文字的内容
- 修改坐标轴的刻度间隔
(更多内容请见:R、ggplot2、shiny 汇总)
初始图样:
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,15,6,18,9,7,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, color = C, group = factor(1))) +
geom_point(size = 3.8) +
geom_line(size = 0.8) +
geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) ## 添加点的数值
p
修改坐标轴的显示范围:
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,15,6,18,9,7,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, color = C, group = factor(1))) +
geom_point(size = 3.8) +
geom_line(size = 0.8) +
geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE)
p + scale_x_continuous(limits = c(-5,15)) ## 方法一
p + xlim(-5,15) ## 方法二
p + xlim(min(dt$A, 0)*1.2, max(dt$A)*1.2) ## 一般使用倍数来限定大小,注意定义最小值的方式
修改坐标轴的标签(内容、大小、字体、颜色、加粗、位置、角度):
library(ggplot2)
windowsFonts(myFont = windowsFont("华文行楷")) ##如果要更改字体,需要先“绑定”字体
dt = data.frame(A = 1:10, B = c(2,15,6,18,9,7,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, color = C, group = factor(1))) +
geom_point(size = 3.8) +
geom_line(size = 0.8) +
geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE)
p + xlab("这是 X 轴") + ylab("这是 Y 轴") + ggtitle("这是标题") ## 修改文字方法一
p + labs(x = "这是 X 轴", y = "这是 Y 轴", title = "这是标题") ## 修改文字方法二
#### 修改 X 轴标签的大小、字体、颜色、加粗、位置、角度 ####
p + xlab("这是 X 轴") + theme(axis.title.x = element_text(size = 15, family = "myFont", color = "green", face = "bold", vjust = 0.5, hjust = 0.5, angle = 45))
## face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗
修改坐标轴刻度的文字(大小、字体、颜色、加粗、位置、角度):
library(ggplot2)
windowsFonts(myFont = windowsFont("华文行楷")) ##如果要更改字体,需要先“绑定”字体
dt = data.frame(A = 1:10, B = c(2,15,6,18,9,7,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, color = C, group = factor(1))) +
geom_point(size = 3.8) +
geom_line(size = 0.8) +
geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE)
####修改 X 轴刻度文字的大小、字体、颜色、加粗、位置、角度:
p + theme(axis.text.x = element_text(size = 15, family = "myFont", color = "green", face = "bold", vjust = 0.5, hjust = 0.5, angle = 45))
## face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗
修改坐标轴刻度文字的内容:
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,15,6,18,9,7,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, color = C, group = factor(1))) +
geom_point(size = 3.8) +
geom_line(size = 0.8) +
geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE)
p + scale_x_continuous(breaks=dt$A, labels = dt$A*1000) ## 只是把刻度上显示的文字改了而已
修改坐标轴的刻度间隔:
library(ggplot2)
dt = data.frame(A = 1:10, B = c(2,15,6,18,9,7,13,15,10,3), C = c(‘A‘,‘C‘,‘A‘,‘B‘,‘C‘,‘D‘,‘A‘,‘C‘,‘D‘,‘B‘))
p = ggplot(dt, aes(x = A, y = B, color = C, group = factor(1))) +
geom_point(size = 3.8) +
geom_line(size = 0.8) +
geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE)
p + scale_x_continuous(breaks=seq(0, 10, 5)) ## X 轴每隔 5 个单位显示一个刻度
版权声明:转载请注明出处,谢谢!
时间: 2024-12-29 15:47:15