自定义不同颜色的ggplot2轴标签
customize ggplot2 axis labels with different colors
我有一个从 ggplot2 创建的基本条形图。 y 变量包含正值和负值,并且大约一半的值向量是负值。我想自定义轴标签,以便当相应 x 因子的 y 值为负时,其标签为红色。这是一个可重现的例子:
#Create data
x <- c("a","b","c","d","e","f")
y <- c("10", "9","-10","11","-3","-15")
data <- data.frame(x, y)
data$y <- as.numeric(as.character(data$y))
data$category <- ifelse(as.numeric(data$y)<0, 0, 1)
data$category <- as.factor(data$category)
#Graph
library(cowplot) #theme
library(ggplot2)
ggplot(data, aes(x=x, y=y)) +
geom_bar(stat = "identity", aes(fill=category)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(axis.text.x = element_text(colour = "black"))
我需要的是一种将 "c"、"e" 和 "f" 的标签颜色更改为我选择的颜色的方法。我尝试切换 theme(aes(axis.text.x=element_text(colour=Air_pricier)))
但这产生了一个错误。提前致谢。
您可以为 theme()
的 axis.text.x
选项提供颜色矢量:
a <- ifelse(data$category == 0, "red", "blue")
ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = "identity", aes(fill = category)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, colour = a))
我也收到@Mark Neal 评论中提到的警告消息;这让我很紧张。这是 ggtext
包的另一种方法。您可以将 x 轴的类别包装在 <span>
s 中并指定您想要的颜色,然后在主题中使用 element_markdown
:
library(ggtext)
library(tidyverse)
data %>%
mutate(x.label = paste("<span style = 'color: ",
ifelse(y > 0, "black", "red"),
";'>",
x,
"</span>", sep = ""),
x.label = fct_reorder(x.label, as.character(x))) %>%
ggplot(aes(x=x.label, y=y)) +
geom_bar(stat = "identity", aes(fill=category)) +
theme(axis.text.x = element_markdown(angle = 45, hjust = 1))
基于 a-s-k 的回答,我使用粘合模板和离散比例将其置于更灵活的形式中。使用此选项,您不必更改数据,而只需在比例尺中定义一个标签器,它会为您做所有事情,如果您想在许多具有不同数据的类似图中为 x 轴着色,这将非常方便。
(在原始问题的情况下,颜色取决于更多的数据,而不仅仅是 x 值,但我想这对某些用户来说仍然很方便。)
library(ggtext)
library(tidyverse)
library(glue)
#Create data
x <- c("a","b","c","d","e","f")
y <- c("10", "9","-10","11","-3","-15")
data <- data.frame(x, y)
data$y <- as.numeric(as.character(data$y))
data$category <- ifelse(as.numeric(data$y)<0, 0, 1)
data$category <- as.factor(data$category)
# create the labels
my_labels <- glue_data(
data,
"<span style='color: {if_else(category==0, 'red', 'blue')}'>{x}</span>"
)
names(my_labels) <- data$x
# plot as you normally would
# use element_markdown as axis.text.x
# and the labels defined before as labels in a discrete scale
ggplot(data, aes(x=x, y=y)) +
geom_bar(stat = "identity", aes(fill=category)) +
theme(
axis.text.x = element_markdown(angle = 45, hjust = 1)
) +
scale_x_discrete(labels=my_labels)
我有一个从 ggplot2 创建的基本条形图。 y 变量包含正值和负值,并且大约一半的值向量是负值。我想自定义轴标签,以便当相应 x 因子的 y 值为负时,其标签为红色。这是一个可重现的例子:
#Create data
x <- c("a","b","c","d","e","f")
y <- c("10", "9","-10","11","-3","-15")
data <- data.frame(x, y)
data$y <- as.numeric(as.character(data$y))
data$category <- ifelse(as.numeric(data$y)<0, 0, 1)
data$category <- as.factor(data$category)
#Graph
library(cowplot) #theme
library(ggplot2)
ggplot(data, aes(x=x, y=y)) +
geom_bar(stat = "identity", aes(fill=category)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(axis.text.x = element_text(colour = "black"))
我需要的是一种将 "c"、"e" 和 "f" 的标签颜色更改为我选择的颜色的方法。我尝试切换 theme(aes(axis.text.x=element_text(colour=Air_pricier)))
但这产生了一个错误。提前致谢。
您可以为 theme()
的 axis.text.x
选项提供颜色矢量:
a <- ifelse(data$category == 0, "red", "blue")
ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = "identity", aes(fill = category)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, colour = a))
我也收到@Mark Neal 评论中提到的警告消息;这让我很紧张。这是 ggtext
包的另一种方法。您可以将 x 轴的类别包装在 <span>
s 中并指定您想要的颜色,然后在主题中使用 element_markdown
:
library(ggtext)
library(tidyverse)
data %>%
mutate(x.label = paste("<span style = 'color: ",
ifelse(y > 0, "black", "red"),
";'>",
x,
"</span>", sep = ""),
x.label = fct_reorder(x.label, as.character(x))) %>%
ggplot(aes(x=x.label, y=y)) +
geom_bar(stat = "identity", aes(fill=category)) +
theme(axis.text.x = element_markdown(angle = 45, hjust = 1))
基于 a-s-k 的回答,我使用粘合模板和离散比例将其置于更灵活的形式中。使用此选项,您不必更改数据,而只需在比例尺中定义一个标签器,它会为您做所有事情,如果您想在许多具有不同数据的类似图中为 x 轴着色,这将非常方便。
(在原始问题的情况下,颜色取决于更多的数据,而不仅仅是 x 值,但我想这对某些用户来说仍然很方便。)
library(ggtext)
library(tidyverse)
library(glue)
#Create data
x <- c("a","b","c","d","e","f")
y <- c("10", "9","-10","11","-3","-15")
data <- data.frame(x, y)
data$y <- as.numeric(as.character(data$y))
data$category <- ifelse(as.numeric(data$y)<0, 0, 1)
data$category <- as.factor(data$category)
# create the labels
my_labels <- glue_data(
data,
"<span style='color: {if_else(category==0, 'red', 'blue')}'>{x}</span>"
)
names(my_labels) <- data$x
# plot as you normally would
# use element_markdown as axis.text.x
# and the labels defined before as labels in a discrete scale
ggplot(data, aes(x=x, y=y)) +
geom_bar(stat = "identity", aes(fill=category)) +
theme(
axis.text.x = element_markdown(angle = 45, hjust = 1)
) +
scale_x_discrete(labels=my_labels)