ggplot2-坐标系篇

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

初始图像

1. 修改坐标轴的标签及标题

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), plot.title = element_text(hjust = 0.5))

## face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗,plot.title主标题居中

2. 修改坐标轴的刻度标签

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)  ## 只是把刻度上显示的文字改了而已

3. 修改坐标轴的刻度标签

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斜体加粗

4. 修改坐标轴的刻度间隔

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 个单位显示一个刻度

5. 去掉网格线

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) +

  labs(x = "", y = "", title = "") +

  theme_bw() +

  geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) +

  theme(panel.grid =element_blank())  ## 删去网格线

p

6. 去掉坐标轴上的刻度标签

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) +

  labs(x = "", y = "", title = "") +

  theme_bw() +

  geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) +

  theme(panel.grid =element_blank()) +    ## 删去网格线

  theme(axis.text = element_blank())  ## 删去所有刻度标签

# theme(axis.text.y = element_blank())  ## 设置 axis.text.y 则只删去 Y 轴的刻度标签,X 轴同理。

p

7. 去掉刻度线

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) +

  labs(x = "", y = "", title = "") +

  theme_bw() +

  geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) +

  theme(panel.grid =element_blank()) +  ## 删去网格线

  theme(axis.text = element_blank()) +  ## 删去所有刻度标签

  theme(axis.ticks = element_blank())  ## 删去所有刻度线

# theme(axis.ticks.y = element_blank())  ## 设置 axis.ticks.y 则只删去 Y 轴的刻度线,X 轴同理。

p

8. 去掉外层边框

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) +

  labs(x = "", y = "", title = "") +

  theme_bw() +

  geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) +

  theme(panel.grid =element_blank()) +  ## 删去网格线

  theme(axis.text = element_blank()) +  ## 删去刻度标签

  theme(axis.ticks = element_blank()) +  ## 删去刻度线

  theme(panel.border = element_blank())  ## 删去外层边框

p

9. 再加上X轴、Y轴(无刻度、无标签)

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) +

  labs(x = "", y = "", title = "") +

  theme_bw() +

  geom_text(aes(label = B, vjust = 1.1, hjust = -0.5, angle = 45), show_guide = FALSE) +

  theme(panel.grid =element_blank()) +  ## 删去网格线

  theme(axis.text = element_blank()) +  ## 删去刻度标签

  theme(axis.ticks = element_blank()) +  ## 删去刻度线

  theme(panel.border = element_blank()) +  ## 删去外层边框

  theme(axis.line = element_line(size=1, colour = "black"))  ## 再加上坐标轴(无刻度、无标签)

p

10. 修改坐标轴的显示范围

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)  ## 一般使用倍数来限定大小,注意定义最小值的方式


参考:https://blog.csdn.net/Bone_ACE/article/details/47427453

你可能感兴趣的:(ggplot2-坐标系篇)