R语言绘图——坐标轴及图例

发布于:2024-10-17 ⋅ 阅读:(13) ⋅ 点赞:(0)

掌握坐标轴与图例的设置与调整,对于提升数据可视化的清晰度和可读性至关重要。通过这些工具,可以有效地传达数据背后的故事,提高图表的表现力。

0x01 坐标轴

一、坐标轴的设置

1、修改坐标轴的标签

ggplot2中,坐标轴是根据数据自动生成的,但是我们可以通过labs()函数为坐标轴添加标签(x轴和y轴的名称)。通常x轴用于表示自变量,y轴用于表示因变量。

library(ggplot2)

# 创建简单散点图,并设置坐标轴的标签
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(x = "Weight of Car (1000 lbs)", y = "Miles per Gallon (MPG)")

2、坐标轴的翻转

有时为了更清晰地展示数据,我们可以将坐标轴翻转,即将x轴和y轴互换。coord_flip()函数可以实现这一点。

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  coord_flip()  # 翻转坐标轴

3、坐标轴范围的控制

通过scale_x_continuous()scale_y_continuous()可以设置坐标轴的数值范围。

  • limits:用来限制x轴或y轴的显示范围。例如,可以通过设置limits只显示部分数据。
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  scale_x_continuous(limits = c(2, 6)) +  # 限制x轴的显示范围
  scale_y_continuous(limits = c(10, 35))  # 限制y轴的显示范围

也可以通过xlimylim来控制。

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  xlim(limits = c(2, 6)) +  # 限制x轴的显示范围
  ylim(limits = c(10, 35))  # 限制y轴的显示范围

4、坐标轴顺序的调整

ggplot(cabbage_exp,aes(x = Date,y = Weight,fill = Cultivar)) +
  geom_bar(position = "dodge",stat = "identity") +
  scale_x_discrete(limits = c("d21","d16","d20"))

5、坐标轴子集的截取

ggplot(cabbage_exp,aes(x = Date,y = Weight,fill = Cultivar)) +
  geom_bar(position = "dodge",stat = "identity") +
  scale_x_discrete(limits = c("d16","d21"))

6、坐标轴的缩放和变换

有时数据的分布较为极端,可能需要对坐标轴进行缩放或变换,ggplot2提供了很多坐标轴变换的方法,如对数变换。

  • 对数变换:使用scale_x_log10()scale_y_log10()对数据进行对数缩放,适合于处理呈指数增长的数据。
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  scale_x_log10()  # 对x轴进行对数缩放

7、修改坐标轴标签的外观

ggplot(cabbage_exp,aes(x = Date,y = Weight,fill = Cultivar)) +
  geom_bar(position = "dodge",stat = "identity") +
  theme(axis.title.x = element_text(face = "italic",colour = "darkred",size = 14))

8、移除坐标轴的标签

ggplot(cabbage_exp,aes(x = Date,y = Weight,fill = Cultivar)) +
  geom_bar(position = "dodge",stat = "identity") +
  theme(axis.title.x = element_blank())

二、刻度线的设置

1、修改刻度线的位置

可以通过breaks参数来控制坐标轴的刻度位置,指定在x轴或y轴上应该显示的刻度值。你可以指定一系列的数字作为刻度。

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  scale_x_continuous(breaks = seq(2, 6, by = 1)) +  # x轴每隔1个单位显示一个刻度
  scale_y_continuous(breaks = seq(10, 35, by = 5))  # y轴每隔5个单位显示一个刻度

2、修改刻度线标签的文本

你可以通过labels参数自定义刻度标签。labels可以是任何类型的字符串向量,用来表示x轴或y轴的刻度标签。

ggplot(mtcars, aes(x = wt, y = factor(cyl))) +
  geom_point() +
  # 自定义x轴的刻度标签
  scale_x_continuous(breaks = seq(2, 6, by = 1),labels = c("Light", "Medium", "Heavy", "Very Heavy", "Extreme")) + 
  # 自定义y轴的类别标签
  scale_y_discrete(labels = c("4" = "Four Cylinders", "6" = "Six Cylinders", "8" = "Eight Cylinders")) +  
  # 设置 x 轴文本的旋转角度为 30 度
  theme(axis.text.x = element_text(angle = 30))

3、移除刻度线的标签

ggplot(mtcars, aes(x = wt, y = factor(cyl))) +
  geom_point() + theme(axis.text.y = element_blank())

0x02 图例

1、修改图例的标题

ggplot2中,图例通常是根据美学映射(如颜色、形状、大小等)自动生成的。你可以通过labs()函数自定义图例的标题。

library(ggplot2)

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +  # 通过气缸数映射颜色
  geom_point(size = 3) +
  labs(color = "Number of Cylinders")  # 设置图例标题

2、修改图例的位置

可以使用theme()函数中的legend.position参数来控制图例的位置。位置可以是“top”、“bottom”、“left”、“right”,或指定坐标位置。

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  theme(legend.position = "bottom")  # 将图例移到图的底部

你也可以使用坐标来精确控制图例位置:

theme(legend.position = c(0.8, 0.2))  # x = 0.8, y = 0.2

3、图例的样式自定义

你可以通过theme()函数对图例的样式进行详细调整,包括字体大小、背景颜色、边框等。

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  theme(
    legend.title = element_text(size = 12, face = "bold"),  # 图例标题字体
    legend.text = element_text(size = 10),  # 图例文本字体
    legend.background = element_rect(fill = "lightgray", size = 0.5, linetype = "solid")  # 图例背景
  )

3、修改图例的标签文字

要更改图例标签文字,你可以在美学映射中使用scale_*函数的labels参数。这可以针对特定的美学(如颜色、形状等)进行设置。

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  scale_color_discrete(labels = c("4 Cylinders", "6 Cylinders", "8 Cylinders")) +  # 自定义图例标签
  labs(color = "Number of Cylinders")

4、调整图例的顺序

要调整图例的顺序,可以在美学映射中将变量转换为有序因子(factor),并设置levels参数来指定显示顺序。

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  scale_color_discrete(labels = c("8 Cylinders", "6 Cylinders", "4 Cylinders")) +  # 自定义图例标签
  labs(color = "Number of Cylinders") +
  scale_color_manual(values = c("red", "green", "blue"))  # 手动设置颜色顺序

如果想改变显示顺序,可以先将cyl转换为有序因子:

mtcars$cyl <- factor(mtcars$cyl, levels = c(8, 6, 4))  # 设置顺序

ggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) +
  geom_point(size = 3) +
  labs(color = "Number of Cylinders")  # 保留标签不变

也可以使用guides(fill = guide_legend(reverse = TRUE))反转图例项的顺序:

# 创建示例数据
data <- data.frame(
  category = c("A", "B", "C"),
  value = c(10, 20, 15),
  fill = c("Group 1", "Group 2", "Group 1")
)

# 绘图
ggplot(data, aes(x = category, y = value, fill = fill)) +
  geom_bar(stat = "identity") +
  guides(fill = guide_legend(reverse = TRUE)) # 反转图例顺序

同理也可以这样使用:

# 创建示例数据
data <- data.frame(
  category = c("A", "B", "C"),
  value = c(10, 20, 15),
  fill = c(TRUE, FALSE, TRUE)
)

# 绘图
ggplot(data, aes(x = category, y = value, fill = factor(fill))) +
  geom_bar(stat = "identity") +
  scale_fill_discrete(breaks = c('TRUE', 'FALSE')) # 控制图例显示的类别

5、移除图例的标题

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  theme(legend.title = element_blank())

6、图例的移除

ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  guides(color = "none")