修复 ggplot 中构面的顺序

问题描述 投票:0回答:7

数据:

df <- data.frame(
    type   = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"), 
    size   = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"),
    amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)

我需要使用 ggplot (x 轴 ->

type
,y 轴 ->
amount
,按
size
分组)绘制上述数据的条形图。当我使用以下代码时,我没有按照数据中显示的顺序获取变量
type
以及
size
。请看图。我为此使用了以下代码。

 ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) + 
  geom_col(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(.~size) + 
  theme_bw() + 
  scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"), 
                    labels = c("T", "F", "P"))

enter image description here.

为了解决顺序问题,我使用了以下变量“类型”的因子方法。另请看图。

temp$new = factor(temp$type, levels=c("T","F","P"), labels=c("T","F","P")) 

enter image description here

但是,现在我不知道如何修复变量的顺序

size
。应该是50%、100%。 150% 和 200%。

r ggplot2 sequence facet
7个回答
201
投票

通过以下方式使您的尺寸成为数据框中的一个因素:

temp$size_f = factor(temp$size, levels=c('50%','100%','150%','200%'))

然后将

facet_grid(.~size)
更改为
facet_grid(.~size_f)

然后绘制: enter image description here

图表现在的顺序是正确的。


48
投票

这里有几个很好的解决方案。

与 Harpal 的答案类似,但在方面内,因此 不需要对基础数据或预绘图操作进行任何更改

# Change this code:
facet_grid(.~size) + 
# To this code:
facet_grid(~factor(size, levels=c('50%','100%','150%','200%')))

这是灵活的,当您更改分面元素时,可以针对任何变量实现,无需对数据进行根本更改。


30
投票

更少的操纵:

facet_grid(~fct_relevel(size,'50%','100%','150%','200%'))


9
投票

这是一个将事物保留在 dplyr 管道链内的解决方案。您预先对数据进行排序,然后使用 mutate_at 转换为因子。我稍微修改了数据,以展示如何在给定可以合理排序的数据的情况下普遍应用该解决方案:

# the data
temp <- data.frame(type=rep(c("T", "F", "P"), 4),
                    size=rep(c("50%", "100%", "200%", "150%"), each=3), # cannot sort this
                    size_num = rep(c(.5, 1, 2, 1.5), each=3), # can sort this
                    amount=c(48.4, 48.1, 46.8, 
                             25.9, 26.0, 24.9,
                             20.8, 21.5, 16.5,
                             21.1, 21.4, 20.1))

temp %>% 
  arrange(size_num) %>% # sort
  mutate_at(vars(size), funs(factor(., levels=unique(.)))) %>% # convert to factor

  ggplot() + 
  geom_bar(aes(x = type, y=amount, fill=type), 
           position="dodge", stat="identity") + 
  facet_grid(~ size)

您也可以应用此解决方案来排列构面内的条形,但您只能选择一个首选顺序:

    temp %>% 
  arrange(size_num) %>%
  mutate_at(vars(size), funs(factor(., levels=unique(.)))) %>%
  arrange(desc(amount)) %>%
  mutate_at(vars(type), funs(factor(., levels=unique(.)))) %>%
  ggplot() + 
  geom_bar(aes(x = type, y=amount, fill=type), 
           position="dodge", stat="identity") + 
  facet_grid(~ size)


  ggplot() + 
  geom_bar(aes(x = type, y=amount, fill=type), 
           position="dodge", stat="identity") + 
  facet_grid(~ size)

6
投票

与 glen_in_boston 的回答类似,但在级别中没有硬编码。

# Change this code:
facet_grid(.~size) + 
# To this code:
facet_grid(~factor(size, levels=unique(df$size)))

之所以有效,是因为数据框中的大小是从最小到最大排列的。

如果大小已经是一个因素,并且您只想在绘图时翻转顺序,这是一个选项:

# Updating dataframe so size is a factor ordered smallest to largest
df <- data.frame(
  type   = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"), 
  size   = factor(c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"), levels=c("50%", "100%","150%","200%"), ordered = TRUE),
  amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)

# Now plotting with facets in the reverse order
ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) + 
  geom_col(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(~factor(size, levels=rev(unique(df$size)))) +  #this part updated
  theme_bw() + 
  scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"), 
                    labels = c("T", "F", "P"))

0
投票

通常,就像在这种情况下一样,指定方面的顺序的愿望源于它们代表一些序数数据。在这种情况下,通常最好首先正确清理数据,即解析字符列中的数值。在这种情况下,可以使用

df$size <- as.numeric(sub("%", "", df$size))/100
轻松完成。然后可以使用带标签的函数来控制方面标签,例如
facet_grid(.~size, labeller = function(x) lapply(x, scales::label_percent()))

library(ggplot2)

df <- data.frame(
  type   = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"), 
  size   = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"),
  amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)

df$size <- as.numeric(sub("%", "", df$size))/100

ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) + 
  geom_col(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(.~size, labeller = function(x) lapply(x, scales::label_percent())) + 
  theme_bw() + 
  scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"), 
                    labels = c("T", "F", "P"))

reprex 包于 2022 年 3 月 11 日创建(v2.0.1)


0
投票

可以自动实现包含数字的字符分面标签的自然排序,无需使用

stringr::str_sort(
, numeric = TRUE)
解析或修改原始数据:

首先定义一个小辅助函数:

as_fct_innatural <- function(x, ordered= TRUE) factor(x, stringr::str_sort(unique(x), numeric = TRUE), ordered = ordered)

然后将其与facet_grid/wrap一起使用,如下所示:

  ... +
  facet_grid(cols = vars(as_fct_innatural( <name of your facet variable>))) +
  ...

完整示例:

library(ggplot2)

df <- data.frame(
  type   = c("T", "F", "P", "T", "F", "P", "T", "F", "P", "T", "F", "P"), 
  size   = c("50%", "50%", "50%", "100%", "100%", "100%", "150%", "150%", "150%", "200%", "200%", "200%"),
  amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 16.5)
)

as_fct_innatural <- function(x, ordered= TRUE) factor(x, stringr::str_sort(unique(x), numeric = TRUE), ordered = ordered)

ggplot(df, aes(type, amount , fill=type, group=type, shape=type, facets=size)) + 
  geom_col(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(cols = vars(as_fct_innatural(size))) + 
  theme_bw() + 
  scale_fill_manual(values = c("darkblue","steelblue1","steelblue4"), 
                    labels = c("T", "F", "P"))

创建于 2023-08-23,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.