使用颜色矢量突出显示 ggplot2 中的前 3 名
Highlight top 3 in ggplot2 with vector of colors
我在下面有一个代码,我想用选定的颜色为前 3 个颜色上色。非常感谢。
library(ggplot2)
library(dplyr)
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
df <- dplyr::mutate(df, top3 = rank(-len) %in% 1:3)
# Basic barplot
p <- ggplot(data = df, aes(x = reorder(dose, -len), y = len)) +
geom_bar(stat = "identity", fill = ifelse(df$top3 == TRUE, c("blue", "yellow", "green"), "grey50")) +
#color = ifelse(df$top3 == TRUE, c("red", "yellow", "green"), "grey50")) +
coord_flip()
p
你可以这样做:
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
df <- df |>
dplyr::arrange(desc(len)) |>
dplyr::mutate(
rank = factor(dplyr::row_number(), labels = dose)
)
fill_colors = c("blue", "yellow", "green")
other_colors = rep("grey50", nrow(df)-length(fill_colors))
my_scale <- c(fill_colors, other_colors)
withr::with_options(
list(ggplot2.discrete.fill = my_scale),
ggplot(data = df, aes(x = reorder(dose, -len), y = len)) +
geom_bar(stat = "identity", aes(fill = rank)) +
scale_fill_discrete() +
coord_flip()
)
您可以在数据框中添加一个额外的颜色排名列,并根据 scale_fill_manual
:
更改颜色
library(ggplot2)
library(dplyr)
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
df <- dplyr::mutate(df, top3 = rank(-len))
df <- dplyr::mutate(df, col_rank = as.character( ifelse(top3 > 3, 4, top3)))
# Basic barplot
p <- ggplot(data = df, aes(x = reorder(dose, -len), y = len, fill = col_rank)) +
geom_bar(stat = "identity") +
coord_flip() +
scale_fill_manual(values = c("1" = "blue", "2" = "yellow", "3" = "green", "4" = "grey50"))
p
与其他建议的选项略有不同,代码更少。我认为您对 if else 语句的想法通常是可以的 - 但是,您在 aes 中使用 $ 似乎很危险。我们可以这样做的原因是顶行的数量与颜色向量的长度相匹配(在这种情况下:3)
library(ggplot2)
library(dplyr)
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
mycols <- c("blue", "yellow", "green")
df <- df %>%
arrange(desc(len)) %>%
mutate(fills = ifelse(row_number() <= length(mycols), mycols, "grey50"))
## swapping x and y makes coord_flip obsolete
## using I in aes is the same as adding "scale_fill_identity" at the end
ggplot(data = df, aes(x = len, y = reorder(dose, -len))) +
geom_col(aes(fill = I(fills)))
由 reprex package (v2.0.1)
于 2022-05-30 创建
我在下面有一个代码,我想用选定的颜色为前 3 个颜色上色。非常感谢。
library(ggplot2)
library(dplyr)
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
df <- dplyr::mutate(df, top3 = rank(-len) %in% 1:3)
# Basic barplot
p <- ggplot(data = df, aes(x = reorder(dose, -len), y = len)) +
geom_bar(stat = "identity", fill = ifelse(df$top3 == TRUE, c("blue", "yellow", "green"), "grey50")) +
#color = ifelse(df$top3 == TRUE, c("red", "yellow", "green"), "grey50")) +
coord_flip()
p
你可以这样做:
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
df <- df |>
dplyr::arrange(desc(len)) |>
dplyr::mutate(
rank = factor(dplyr::row_number(), labels = dose)
)
fill_colors = c("blue", "yellow", "green")
other_colors = rep("grey50", nrow(df)-length(fill_colors))
my_scale <- c(fill_colors, other_colors)
withr::with_options(
list(ggplot2.discrete.fill = my_scale),
ggplot(data = df, aes(x = reorder(dose, -len), y = len)) +
geom_bar(stat = "identity", aes(fill = rank)) +
scale_fill_discrete() +
coord_flip()
)
您可以在数据框中添加一个额外的颜色排名列,并根据 scale_fill_manual
:
library(ggplot2)
library(dplyr)
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
df <- dplyr::mutate(df, top3 = rank(-len))
df <- dplyr::mutate(df, col_rank = as.character( ifelse(top3 > 3, 4, top3)))
# Basic barplot
p <- ggplot(data = df, aes(x = reorder(dose, -len), y = len, fill = col_rank)) +
geom_bar(stat = "identity") +
coord_flip() +
scale_fill_manual(values = c("1" = "blue", "2" = "yellow", "3" = "green", "4" = "grey50"))
p
与其他建议的选项略有不同,代码更少。我认为您对 if else 语句的想法通常是可以的 - 但是,您在 aes 中使用 $ 似乎很危险。我们可以这样做的原因是顶行的数量与颜色向量的长度相匹配(在这种情况下:3)
library(ggplot2)
library(dplyr)
df <- data.frame(dose = c("D0.5", "D1", "D2", "D3", "D4", "D5"),
len = c(4.2, 10, 29.5, 5, 7, 15))
mycols <- c("blue", "yellow", "green")
df <- df %>%
arrange(desc(len)) %>%
mutate(fills = ifelse(row_number() <= length(mycols), mycols, "grey50"))
## swapping x and y makes coord_flip obsolete
## using I in aes is the same as adding "scale_fill_identity" at the end
ggplot(data = df, aes(x = len, y = reorder(dose, -len))) +
geom_col(aes(fill = I(fills)))
由 reprex package (v2.0.1)
于 2022-05-30 创建