棒棒糖图表:使用 R 根据列名的升序值分配颜色
Lollipop Chart: Assign Colours According to the Ascending Values of a Column Name Using R
我在数据框中有一列 (RMSE),我希望 lollipop chart
的颜色(绿色、黄绿色、黄色、橙色、红色)根据列 RMSE
。我希望代表最低 RMSE 值的 pop 为 green
,而代表最高 RMSE 值的 pop 为`red
df1 <- read.table(text =
"nbb_RMSE 9 0.2402482
mbb_RMSE 9 0.1023012
cbb_RMSE 8 0.2031448
tmbb_RMSE 4 0.2654746
tcbb_RMSE 9 0.4048711")
colnames(df1) <- c("Methods", "lb", "RMSE")
df1 |>
dplyr::mutate(lb = as.factor(lb)) |>
ggplot2::ggplot(ggplot2::aes(x = Methods, y = RMSE, colour = Methods)) +
ggplot2::geom_point(size = 4) +
ggplot2::geom_segment(ggplot2::aes(x = Methods, xend = Methods, yend = RMSE, y = 0)) +
ggplot2::scale_color_manual(values = c("green", "yellowgreen", "yellow", "orange", "red")) +
ggplot2::theme_bw()
这是
一个选项是使用命名颜色向量,您使用 reorder
根据 RMSE
的值分配名称:
library(ggplot2)
pal <- c("green", "yellowgreen", "yellow", "orange", "red")
names(pal) <- levels(reorder(df1$Methods, df1$RMSE))
df1 |>
dplyr::mutate(lb = as.factor(lb)) |>
ggplot2::ggplot(ggplot2::aes(x = Methods, y = RMSE, colour = Methods)) +
ggplot2::geom_point(size = 4) +
ggplot2::geom_segment(ggplot2::aes(x = Methods, xend = Methods, yend = RMSE, y = 0)) +
ggplot2::scale_color_manual(values = pal) +
ggplot2::theme_bw()
@stephan 选项的类似方法:
library(tidyverse)
df1 <- read.table(text =
"nbb_RMSE 9 0.2402482
mbb_RMSE 9 0.1023012
cbb_RMSE 8 0.2031448
tmbb_RMSE 4 0.2654746
tcbb_RMSE 9 0.4048711")
colnames(df1) <- c("Methods", "lb", "RMSE")
df1 |>
mutate(colour = fct_reorder(Methods, RMSE)) |>
ggplot(aes(Methods, RMSE, colour = colour)) +
geom_point(size = 4) +
geom_segment(aes(Methods, xend = Methods, yend = RMSE, y = 0)) +
scale_color_manual(values = c("green", "yellowgreen", "yellow", "orange", "red")) +
theme_bw()
由 reprex package (v2.0.1)
创建于 2022-06-02
我在数据框中有一列 (RMSE),我希望 lollipop chart
的颜色(绿色、黄绿色、黄色、橙色、红色)根据列 RMSE
。我希望代表最低 RMSE 值的 pop 为 green
,而代表最高 RMSE 值的 pop 为`red
df1 <- read.table(text =
"nbb_RMSE 9 0.2402482
mbb_RMSE 9 0.1023012
cbb_RMSE 8 0.2031448
tmbb_RMSE 4 0.2654746
tcbb_RMSE 9 0.4048711")
colnames(df1) <- c("Methods", "lb", "RMSE")
df1 |>
dplyr::mutate(lb = as.factor(lb)) |>
ggplot2::ggplot(ggplot2::aes(x = Methods, y = RMSE, colour = Methods)) +
ggplot2::geom_point(size = 4) +
ggplot2::geom_segment(ggplot2::aes(x = Methods, xend = Methods, yend = RMSE, y = 0)) +
ggplot2::scale_color_manual(values = c("green", "yellowgreen", "yellow", "orange", "red")) +
ggplot2::theme_bw()
这是
一个选项是使用命名颜色向量,您使用 reorder
根据 RMSE
的值分配名称:
library(ggplot2)
pal <- c("green", "yellowgreen", "yellow", "orange", "red")
names(pal) <- levels(reorder(df1$Methods, df1$RMSE))
df1 |>
dplyr::mutate(lb = as.factor(lb)) |>
ggplot2::ggplot(ggplot2::aes(x = Methods, y = RMSE, colour = Methods)) +
ggplot2::geom_point(size = 4) +
ggplot2::geom_segment(ggplot2::aes(x = Methods, xend = Methods, yend = RMSE, y = 0)) +
ggplot2::scale_color_manual(values = pal) +
ggplot2::theme_bw()
@stephan 选项的类似方法:
library(tidyverse)
df1 <- read.table(text =
"nbb_RMSE 9 0.2402482
mbb_RMSE 9 0.1023012
cbb_RMSE 8 0.2031448
tmbb_RMSE 4 0.2654746
tcbb_RMSE 9 0.4048711")
colnames(df1) <- c("Methods", "lb", "RMSE")
df1 |>
mutate(colour = fct_reorder(Methods, RMSE)) |>
ggplot(aes(Methods, RMSE, colour = colour)) +
geom_point(size = 4) +
geom_segment(aes(Methods, xend = Methods, yend = RMSE, y = 0)) +
scale_color_manual(values = c("green", "yellowgreen", "yellow", "orange", "red")) +
theme_bw()
由 reprex package (v2.0.1)
创建于 2022-06-02