R 中的 scale_colour_discrete 函式
本文將介紹如何在 R 中使用 scale_colour_discrete
。
使用 scale_colour_discrete
修改 R 中的 colour
比例標籤
scale_colour_discrete
可用於修改通常需要具有離散值的 colour
比例標籤。在這種情況下,我們演示了一個使用過濾後的 babynames
資料集的示例,從中僅提取了五個名字以使其更簡單。我們使用 geom_line 繪製線圖。x
/y
軸上的對映變數是嬰兒的數量和他們出生的年份。由於名稱對映到 colour
比例,它們將有一個圖例,其中不同的顏色被對映到每個其中。使用 scale_colour_discrete
函式的 labels
引數,我們可以傳遞一個值向量,我們可以用它來替換現有的比例標籤。
library(ggplot2)
library(gridExtra)
library(babynames)
library(dplyr)
dat <- babynames %>%
filter(name %in% c("Alice", "Maude", "Mae",
"Annie", "Ella")) %>% filter(sex=="F")
p3 <- ggplot(dat, aes(x = year, y = n, colour = name)) +
geom_line() +
scale_colour_discrete(
labels = c("Al", "Mau", "Mae", "An", "El")) +
scale_y_continuous(
breaks = seq(0, 13000, 1000),
name = "Number of babies") +
scale_x_continuous(
breaks = seq(1880, 2017, 12),
name = "Year") +
theme(
legend.position = "left",
legend.background = element_rect(fill = "white", colour = "black"),
plot.title = element_text(
size = rel(1.2), lineheight = .9,
family = "Calibri", face = "bold", colour = "brown"
)) +
ggtitle("Name Popularity Through Years")
p3
使用 scale_colour_discrete
修改 R 中的 colour
比例名稱
scale_colour_discrete
函式的另一個有用引數是 name
,它控制 colour
刻度標題。自定義字串值可以分配給 name
引數,這會在以下示例程式碼中生成修改後的圖例名稱。
library(ggplot2)
library(gridExtra)
library(babynames)
library(dplyr)
dat <- babynames %>%
filter(name %in% c("Alice", "Maude", "Mae",
"Annie", "Ella")) %>% filter(sex=="F")
p3 <- ggplot(dat, aes(x = year, y = n, colour = name)) +
geom_line() +
scale_colour_discrete(
name = "Name",
labels = c("Al", "Mau", "Mae", "An", "El")) +
scale_y_continuous(
breaks = seq(0, 13000, 1000),
name = "Number of babies") +
scale_x_continuous(
breaks = seq(1880, 2017, 12),
name = "Year") +
theme(
legend.position = "left",
legend.background = element_rect(fill = "white", colour = "black"),
plot.title = element_text(
size = rel(1.2), lineheight = .9,
family = "Calibri", face = "bold", colour = "brown"
)) +
ggtitle("Name Popularity Through Years")
p3
前面的圖在單個面板上繪製所有線條,但我們可以使用 facet_wrap
函式在單獨的一個面板上輸出每條線。facet_wrap
將需要對映的變數作為第一個引數。
library(ggplot2)
library(gridExtra)
library(babynames)
library(dplyr)
dat <- babynames %>%
filter(name %in% c("Alice", "Maude", "Mae",
"Annie", "Ella")) %>% filter(sex=="F")
p4 <- ggplot(dat, aes(x = year, y = n, colour = name)) +
geom_line() +
scale_colour_discrete(
name = "Name",
labels = c("Al", "Mau", "Mae", "An", "El")) +
scale_y_continuous(
breaks = seq(0, 13000, 1000),
name = "Number of babies") +
scale_x_continuous(
breaks = seq(1880, 2017, 12),
name = "Year") +
theme(
legend.position = "left",
legend.background = element_rect(fill = "white", colour = "black"),
plot.title = element_text(
size = rel(1.2), lineheight = .9,
family = "Calibri", face = "bold", colour = "brown"
)) +
ggtitle("Name Popularity Through Years") +
facet_wrap(vars(name))
p4
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn