ggplot2——坐标系篇

作者: Liam_ml | 来源:发表于2018-11-30 00:26 被阅读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
image.png

如何修改坐标轴的显示范围:

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

如何修改坐标轴的标签(内容、大小、字体、颜色、加粗、位置、角度):

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

如何修改坐标轴的刻度标签(内容):

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

如何修改坐标轴的刻度标签(大小、字体、颜色、加粗、位置、角度):

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

如何修改坐标轴的刻度间隔:

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

如何去掉网格线:

删去之前:

image.png

删去之后:

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

image.png

如何去掉坐标轴上的刻度标签:

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
 
image.png

如何去掉刻度线:

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
 
image.png

如何去掉外层边框:

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
image.png

如何再加上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
 
image.png

相关文章

  • R 数据可视化 —— ggplot 坐标系

    前言 ggplot2 包含 6 种不同的坐标系: coord_cartesian: 笛卡尔坐标系 coord_fi...

  • ggplot2——坐标系篇

    初始图样: 如何修改坐标轴的显示范围: 如何修改坐标轴的标签(内容、大小、字体、颜色、加粗、位置、角度): 如何修...

  • 可视化 坐标系统

    坐标系可能是ggplot2中最复杂的部分。 默认坐标系是笛卡尔坐标系,其中x和y位置独立地确定每个点的位置。 还有...

  • [11] 《R数据科学》坐标系

    坐标系可能是ggplot2中最复杂的部分。默认的坐标系是笛卡尔直角坐标系,可以通过其独立作用的x坐标和y坐标找到每...

  • R语言_ggplot2

    关于ggplot2包的应用。 数据结构、绘图对象、图层、标度变换、分面、坐标系、主题; 数据结构 一个图形对象,由...

  • R语言之ggplot2画图篇

    R语言之ggplot2画图篇

  • R语言_ggplot2绘图

    ggplot2语法 入门级绘图模版属性设置分面几何对象统计变换位置调整坐标系 以上是各种参数,需要的时候用一下就可...

  • ggplot2坐标系列的调整

    ggplot(kegg_class,aes(x = Description,y = Count)) + geom_...

  • 微信小程序坐标系纠正,计算偏差整理

    这是一篇关于WGS-84坐标系,GCJ-02坐标系,BD09坐标系相互转换的文章,方法都是网上查询得到的,并非原创...

  • 【GIS】坐标系

      坐标系应该算是GIS专业的基础知识了,今天决定水文一篇,介绍一下坐标系。 1.基础知识 地理坐标系 (GCS)...

网友评论

    本文标题:ggplot2——坐标系篇

    本文链接:https://www.haomeiwen.com/subject/ustccqtx.html