如何使用 waffle::geom_waffle() 选择填充变量的级别顺序
How to choose the levels order of the fill variable with waffle::geom_waffle()
我不明白如何在 R waffle::geom_waffle() 中更改填充变量的级别顺序。
似乎对变量进行排序是无效的,因为它总是按观察次数排序。
# First example
db_graph <- data.table::data.table(group = c("A", "B"),
n = c(13, 25))
ggplot2::ggplot(data = db_graph,
mapping = ggplot2::aes(fill = group,
values = n)) +
waffle::geom_waffle(flip = TRUE)
# Second example
db_graph <- db_graph[j = group := forcats::fct_rev(group)]
ggplot2::ggplot(data = db_graph,
mapping = ggplot2::aes(fill = group,
values = n)) +
waffle::geom_waffle(flip = TRUE)
您可以通过使用例如重新排序数据框来实现您想要的结果setorder(db_graph, group)
。但你是对的。我也预料到顺序是由因子水平的顺序决定的。
library(ggplot2)
library(data.table)
library(waffle)
library(forcats)
db_graph <- data.table::data.table(
group = c("A", "B"),
n = c(13, 25)
)
db_graph <- db_graph[j = group := fct_rev(group)]
ggplot(
data = db_graph,
mapping = aes(
fill = group,
values = n
)
) +
geom_waffle(flip = TRUE)
setorder(db_graph, group)
ggplot(
data = db_graph,
mapping = aes(
fill = group,
values = n
)
) +
geom_waffle(flip = TRUE)
使用 tidyverse,您可以使用 arrange()。
db_graph <- tibble(
group = c("A", "B"),
n = c(13, 25)
)
db_graph %>%
arrange(group) %>%
ggplot(aes(fill = group,
values = n)) +
geom_waffle(flip = TRUE)
我不明白如何在 R waffle::geom_waffle() 中更改填充变量的级别顺序。
似乎对变量进行排序是无效的,因为它总是按观察次数排序。
# First example
db_graph <- data.table::data.table(group = c("A", "B"),
n = c(13, 25))
ggplot2::ggplot(data = db_graph,
mapping = ggplot2::aes(fill = group,
values = n)) +
waffle::geom_waffle(flip = TRUE)
# Second example
db_graph <- db_graph[j = group := forcats::fct_rev(group)]
ggplot2::ggplot(data = db_graph,
mapping = ggplot2::aes(fill = group,
values = n)) +
waffle::geom_waffle(flip = TRUE)
您可以通过使用例如重新排序数据框来实现您想要的结果setorder(db_graph, group)
。但你是对的。我也预料到顺序是由因子水平的顺序决定的。
library(ggplot2)
library(data.table)
library(waffle)
library(forcats)
db_graph <- data.table::data.table(
group = c("A", "B"),
n = c(13, 25)
)
db_graph <- db_graph[j = group := fct_rev(group)]
ggplot(
data = db_graph,
mapping = aes(
fill = group,
values = n
)
) +
geom_waffle(flip = TRUE)
setorder(db_graph, group)
ggplot(
data = db_graph,
mapping = aes(
fill = group,
values = n
)
) +
geom_waffle(flip = TRUE)
使用 tidyverse,您可以使用 arrange()。
db_graph <- tibble(
group = c("A", "B"),
n = c(13, 25)
)
db_graph %>%
arrange(group) %>%
ggplot(aes(fill = group,
values = n)) +
geom_waffle(flip = TRUE)