在ggplot2中,当有一个facet_wrap时,如何使所有geom_col条具有相同的值时都具有相同的颜色?
In ggplot2, when there is a facet_wrap, how to make all geom_col bars the same color when they have the same value?
我有一个包含三列的数据框 df
:TASK
、CONDITION
和 SCORE
。
我要表示数据:
- 作为条形图(我正在使用
geom_col
)
- 每个
TASK
都有一个单独的图(我正在使用
facet_wrap(~TASK)
)
- 每个
CONDITION
都有一个单独的栏(我
使用 ggplot(df, aes(x=CONDITION))
)
此外,预期的行为是,如果给定条形图的数据总和达到给定百分比,则该条形图应与达到相同百分比的其他条形图颜色相同。不幸的是,我无法让它工作。
在下面的最小示例中,有 3 个柱达到 100%,因此我希望它们按照说明都是蓝色的 high="blue"
,但实际情况并非如此。
Input =("
TASK CONDITION SCORE
GAU 0 0.25
GAU 0 0.25
GAU 0 0.25
GAU 0 0.25
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
PLN 0 0.3333
PLN 0 0.3333
PLN 0 0
PLN 1 0.5
PLN 1 0.5
")
df <- read.table(textConnection(Input),
header=TRUE)
df$CONDITION <- factor(df$CONDITION)
library(ggplot2)
ggplot(df, aes(x=CONDITION, y=SCORE, fill=SCORE)) +
geom_col() +
ggtitle("Performance") +
ylab("Total") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~TASK) +
scale_fill_gradient(low="red", high="blue")
真正发生的事情有点被情节隐藏了。如果我们在条上加一个边框并更改第一个值,也许会使它更清晰
df2 <- df
df2[1, "SCORE"] <- .5
ggplot(df2, aes(x=CONDITION, y=SCORE, fill=SCORE)) +
geom_col(color="black") +
ggtitle("Performance") +
ylab("Total") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~TASK) +
scale_fill_gradient(low="red", high="blue")
它不是按图的总高度着色,而是按每次观察着色。请注意您的色标如何只上升到 0.5。如果您只想为此使用 ggplot,则可以使用 geom_bar 的摘要统计信息来为您进行汇总。它看起来像这样
ggplot(df, aes(x=CONDITION, y=SCORE, fill=..y..)) +
geom_bar(stat="summary", fun.y="sum") +
ggtitle("Performance") +
ylab("Total") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~TASK) +
scale_fill_gradient(low="red", high="blue")
我有一个包含三列的数据框 df
:TASK
、CONDITION
和 SCORE
。
我要表示数据:
- 作为条形图(我正在使用
geom_col
) - 每个
TASK
都有一个单独的图(我正在使用facet_wrap(~TASK)
) - 每个
CONDITION
都有一个单独的栏(我 使用ggplot(df, aes(x=CONDITION))
)
此外,预期的行为是,如果给定条形图的数据总和达到给定百分比,则该条形图应与达到相同百分比的其他条形图颜色相同。不幸的是,我无法让它工作。
在下面的最小示例中,有 3 个柱达到 100%,因此我希望它们按照说明都是蓝色的 high="blue"
,但实际情况并非如此。
Input =("
TASK CONDITION SCORE
GAU 0 0.25
GAU 0 0.25
GAU 0 0.25
GAU 0 0.25
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
PLN 0 0.3333
PLN 0 0.3333
PLN 0 0
PLN 1 0.5
PLN 1 0.5
")
df <- read.table(textConnection(Input),
header=TRUE)
df$CONDITION <- factor(df$CONDITION)
library(ggplot2)
ggplot(df, aes(x=CONDITION, y=SCORE, fill=SCORE)) +
geom_col() +
ggtitle("Performance") +
ylab("Total") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~TASK) +
scale_fill_gradient(low="red", high="blue")
真正发生的事情有点被情节隐藏了。如果我们在条上加一个边框并更改第一个值,也许会使它更清晰
df2 <- df
df2[1, "SCORE"] <- .5
ggplot(df2, aes(x=CONDITION, y=SCORE, fill=SCORE)) +
geom_col(color="black") +
ggtitle("Performance") +
ylab("Total") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~TASK) +
scale_fill_gradient(low="red", high="blue")
它不是按图的总高度着色,而是按每次观察着色。请注意您的色标如何只上升到 0.5。如果您只想为此使用 ggplot,则可以使用 geom_bar 的摘要统计信息来为您进行汇总。它看起来像这样
ggplot(df, aes(x=CONDITION, y=SCORE, fill=..y..)) +
geom_bar(stat="summary", fun.y="sum") +
ggtitle("Performance") +
ylab("Total") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~TASK) +
scale_fill_gradient(low="red", high="blue")