查看原文
其他

R可视化06|ggplot2图层-标度图层(scale layer)-坐标轴篇

pythonic生物人 pythonic生物人 2022-10-25

"pythonic生物人"的第104篇分享

ggplot2标度图层(scale layer)可简单理解为图形中辅助您读图的一些组件,如不同颜色、形状、线性、大小、图例、坐标轴名称等等,详细介绍为。Scales in ggplot2 control the mapping from data to aesthetics. They take your data and turn it into something that you can see, like size, colour, position or shape. They also provide the tools that let you interpret the plot: the  axes and legends. You can generate plots with ggplot2 without knowing how scales work, but understanding scales and learning how to manipulate them will give you much more control.In ggplot2, guides are produced automatically based on the layers in your plot. You don’t directly control the legends and axes; instead you set up the data so that there’s a clear mapping between data and aesthetics, and a guide is generated for you. This is very different to base R graphics, where you have total control over the legend, and can be frustrating when you first start using ggplot2. However, once you get the hang of it, you’ll find that it saves you time, and there is little you cannot do.Divide scales into three main groups:

  • Position scales and axes.(位置标度和坐标轴)
  • Colour scales and legends.(颜色标度和图例)
  • Other scales for non-position aesthetics.(其它非位置图像属性标度)

本文详细介绍ggplot2位置标度和坐标轴设置 (Position scales and axes)。

建议:左右上下对照来阅读,查看修改带来的变动。

续前篇:


本文目录

1、标度名称都是什么?
2、位置标度和坐标轴 (Position scales and axes)
数值型变量(Numeric)位置标度和坐标轴
时间型变量(Date-time)位置标度和坐标轴
离散型变量(Discrete)位置标度和坐标轴
箱型变量(Binned)位置标度和坐标轴
设置显示范围(Limits)

1、标度名称都是什么?

options(repr.plot.width = 7, repr.plot.height = 7, repr.plot.res = 300)
toy <- data.frame(
  const = 1
  up = 1:4,
  txt = letters[1:4], 
  big = (1:4)*1000,
  log = c(25102000)
)

base <- ggplot(toy, aes(up, up)) + geom_point()

#添加x轴名称
p1 <- base + scale_x_continuous("X axis")

#x轴换个名称
p2 <- base + scale_x_continuous(quote(a + mathematical ^ expression))#quote()添加数学表达式


library('ggtext')
p3 <- ggplot(toy, aes(up, up)) + 
  geom_point() + 
  scale_x_continuous("Axis title with *italics* and **boldface**")

#ggtext模块助x轴名称使用斜体和加粗:该模块专助ggplot2 text渲染
p4 <- p3 + theme(axis.title.x = ggtext::element_markdown())

#labs函数便利设置坐标轴及图里名称
p5 <- ggplot(toy, aes(const, up)) + 
  geom_point(aes(colour = txt)) + 
  labs(
    x = "X axis"
    y = quote(Y^axis), 
    colour = "Colour\nlegend",
    title = "A pithy title",
    subtitle = "A more precise subtitle"
  )

p6 <- grid.arrange(p1,p2,p3,p4,p5,nrow = 3)
ggsave("scale1.png", p6, width = 8, height = 13)

2、位置标度和坐标轴 (Position scales and axes)

  • 数值型变量(Numeric)位置标度和坐标轴

介绍x,y轴名称、刻度(主刻度,次刻度)、刻度值、刻度值单位、刻度间隔、

options(repr.plot.width = 5, repr.plot.height = 15, repr.plot.res = 300)
# 默认位置标度scale_x_continuous()和scale_y_continuous()
p1 <- ggplot(mpg, aes(displ, hwy)) + geom_point() + 
    scale_x_continuous() + scale_y_continuous()

#x轴刻度颠倒scale_x_reverse()
p2 <- p1 + scale_x_reverse()

#y轴刻度颠倒scale_y_reverse()
p3 <- p2 + scale_y_reverse()

p4 <- ggplot(mpg, aes(drv, hwy)) + 
  geom_hline(yintercept = 28, colour = "red") + 
  geom_boxplot() 

#设置y轴范围ylim,显示部分观测值
p5 <- p4 + coord_cartesian(ylim = c(1035)) 

#直接移除ylim外的值
p6 <- p5 + ylim(1035


df <- data.frame(x = 1:6, y = 8:13)
p7 <- ggplot(df, aes(x, y)) + 
  geom_col(aes(fill = x)) +                    
  geom_vline(xintercept = 3.5, colour = "red"# for visual clarity only

#默认scales::censor()将limits外的设置为NA用灰色填充
p9 <- p7 + scale_fill_gradient(limits = c(13))

#scales::squish将limits外的设置为一个区间
p10 <- p7 + scale_fill_gradient(limits = c(13), oob = scales::squish)


toy <- data.frame(
  const = 1
  up = 1:4,
  txt = letters[1:4], 
  big = (1:4)*1000,
  log = c(25102000)
)

p11 <- ggplot(toy, aes(big, const)) + 
  geom_point() + 
  labs(x = NULL, y = NULL)

#设置刻度之间间距
p12 <- p11 + scale_x_continuous(breaks = scales::breaks_width(800))

#设置刻度右偏移200
p13 <- p11 + scale_x_continuous(breaks = scales::breaks_width(800, offset = 200))

#设置刻度左偏移-200
p14 <- p11 + scale_x_continuous(breaks = scales::breaks_width(800, offset = -200))

#关闭刻度值
p15 <- p11 + scale_x_continuous(breaks = NULL)


#设置次要刻度minor_breaks
mb <- unique(as.numeric(1:10 %o% 10 ^ (0:3)))
log_base <- ggplot(toy, aes(log, const)) + geom_point()

p16 <- log_base + scale_x_log10()
p17 <- log_base + scale_x_log10(minor_breaks = mb)

#自定义刻度值
p18 <- p11 + scale_x_continuous(breaks = c(20004000), labels = c("2k""4k"))

#默认添加百分号单位刻度标签
p19 <- p11 + scale_y_continuous(labels = scales::label_percent())

#suffix指定其它单位刻度标签
p20 <- p11 + scale_y_continuous(labels = scales::label_dollar(prefix = "", suffix = "€"))

#只显示刻度,不显示标签
p21 <- p11 + scale_x_continuous(labels = NULL)


p22 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,nrow = 10)
#p22
ggsave("scale2.png", p22, width = 5, height = 15)

  • 时间型变量(Date-time)位置标度和坐标轴

options(repr.plot.width = 8, repr.plot.height = 10, repr.plot.res = 300)
p1 <- ggplot(economics, aes(date, psavert)) + 
  geom_line(na.rm = TRUE) +
  labs(x = NULL, y = NULL)

#设置日期型坐标间隔
p2 <- p1 + scale_x_date(date_breaks = "25 years")


#指定刻度标签
p3 <- p1 + scale_x_date(
  limits = as.Date(c("2003-01-01""2003-04-01")),
  date_breaks = "1 month"
)

#设置次要日期刻度
p4 <- p1 + scale_x_date(
  limits = as.Date(c("2003-01-01""2003-04-01")),
  date_breaks = "1 month",
  date_minor_breaks = "1 week"#设置次要日期刻度
)


#设置日期单位,各种个性化
base <- ggplot(economics, aes(date, psavert)) + 
  geom_line(na.rm = TRUE) +
  labs(x = NULL, y = NULL)

p5 <- base + scale_x_date(date_breaks = "5 years")
p6 <- base + scale_x_date(date_breaks = "5 years", date_labels = "%y")#以年为单位

lim <- as.Date(c("2004-01-01""2005-01-01"))

p7 <- base + scale_x_date(limits = lim, date_labels = "%b %y")
p8 <- base + scale_x_date(limits = lim, date_labels = "%B\n%Y")

p9 <- base + scale_x_date(labels = scales::label_date("%b %y"))
p10 <- base + scale_x_date(limits = lim, labels = scales::label_date_short())#首尾设置单位

p11 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,nrow = 5)
p11
ggsave("scale2.png", p11, width = 7, height = 12)

  • 离散型变量(Discrete)位置标度和坐标轴

options(repr.plot.width = 9, repr.plot.height = 12, repr.plot.res = 300)
#默认scale_x_discrete() 和scale_y_discrete()
p1 <- ggplot(mpg, aes(x = hwy, y = class)) + 
  geom_point() + 
  scale_x_continuous() +
  scale_y_discrete()

#annotate设置刻度标签
p2 <- ggplot(mpg, aes(x = hwy, y = class)) + 
  geom_point() +
  annotate("text", x = 5, y = 1:7, label = 1:7)

p3 <- ggplot(toy, aes(const, txt)) + 
  geom_point() +
  labs(x = NULL, y = NULL)

#因子指定刻度标签
p4 <- p3 + scale_y_discrete(labels = c(c = "carrot", b = "banana"))

base <- ggplot(mpg, aes(manufacturer, hwy)) + geom_boxplot() 
p5 <- base

#刻度标签错位显示
p6 <- base + guides(x = guide_axis(n.dodge = 3))

#刻度标签倾斜
p7 <- base + guides(x = guide_axis(angle = 90))

p8 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,nrow = 4)
p8
ggsave("scale3.png", p8, width = 7, height = 12)

  • 箱型变量(Binned)位置标度和坐标轴

options(repr.plot.width = 9, repr.plot.height = 3, repr.plot.res = 300)
p1 <- ggplot(mpg, aes(hwy, class)) + geom_count()
#设置每个位置想展示的观测值个数
p2 <- p1 + scale_x_binned(n.breaks = 5)#只展示5个观测值
grid.arrange(p1,p2,nrow = 1)

  • 设置显示范围(Limits)

options(repr.plot.width = 8, repr.plot.height = 10, repr.plot.res = 300)
#mpg按条件过滤
mpg_99 <- mpg %>% filter(year == 1999)
mpg_08 <- mpg %>% filter(year == 2008)

base_99 <- ggplot(mpg_99, aes(displ, hwy, colour = fl)) + geom_point() 
base_08 <- ggplot(mpg_08, aes(displ, hwy, colour = fl)) + geom_point() 

#按条件分开设置
p1 <- base_99
p2 <- base_08

#设置x、y刻度范围
p3 <- base_99 + 
  scale_x_continuous(limits = c(17)) +
  scale_y_continuous(limits = c(1045))

p4 <- base_08 + 
  scale_x_continuous(limits = c(17)) +
  scale_y_continuous(limits = c(1045))

#修改颜色标度scale_color_discrete
p5 <- base_99 + 
  scale_x_continuous(limits = c(17)) +
  scale_y_continuous(limits = c(1045)) +
  scale_color_discrete(limits = c("c""d""e""p""r"))

p6 <- base_08 + 
  scale_x_continuous(limits = c(17)) +
  scale_y_continuous(limits = c(1045)) +
  scale_color_discrete(limits = c("c""d""e""p""r"))

#直接传xlim,ylim参数
p7 <- base_99 + xlim(17) + ylim(1045)
p8 <- base_08 + xlim(17) + ylim(1045)

#直接传colour参数,注意比较scale_color_discrete
p9 <- base_99 + lims(x = c(17), y = c(1045), colour = c("c""d""e""p""r"))
p10 <- base_08 + lims(x = c(17), y = c(1045), colour = c("c""d""e""p""r"))

p11 <- ggplot(mpg, aes(displ, hwy, colour = fl)) + 
  geom_point() +
  facet_wrap(vars(year))

p12 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,nrow = 6)
p12
ggsave("scale4.png", p12, width = 8, height = 10)

本文结束,更多好文:

有用请“点赞”“在看”“分享”

有意见请移步到QQ群629562529反馈,一起进步哈!

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存