当变量为因数时,geom_bar更改行为

时间:2019-05-29 12:14:00

标签: r ggplot2

为两列数据框绘制条形图就可以了:

library(tidyverse)
tibble(.group = c("A","B","A","B","B","A","A","A","A") %>% factor,
       .value = c( 1L, 1L, 3L, 3L, 2L, 2L, 3L, 3L, 1L)) %>%
ggplot() +
  geom_bar(aes(x=.value, y=..prop.., fill=.group), position="dodge")

Click me. I cannot embed images :(

但是,将变量声明为因子时,geom_bar会产生无意义的结果。在这种特殊情况下怎么了?

library(tidyverse)
tibble(.group = c("A","B","A","B","B","A","A","A","A") %>% factor,
       .value = c( 1L, 1L, 3L, 3L, 2L, 2L, 3L, 3L, 1L) %>% factor) %>%
ggplot() +
  geom_bar(aes(x=.value, y=..prop.., fill=.group), position="dodge")

Click me

1 个答案:

答案 0 :(得分:1)

当x变量为数字时,geom_bar计算相对于x变量所有值的比例。如果x变量是分类变量(字符类或因子类),则geom_bar将计算x变量每个值在 之内的比例。您可以通过添加group美学来强制geom_bar计算相对于所有x值的比例来覆盖此行为。例如:

dat = tibble(.group = c("A","B","A","B","B","A","A","A","A") %>% factor,
             .value = c( 1L, 1L, 3L, 3L, 2L, 2L, 3L, 3L, 1L))

# .value is integer
ggplot(dat) +
  geom_bar(aes(x=.value, y=..prop.., fill=.group), position="dodge")

# .value is factor
ggplot(dat %>% mutate(.value=factor(.value))) +
  geom_bar(aes(x=.value, y=..prop.., fill=.group), position="dodge")

ggplot(dat %>% mutate(.value=factor(.value))) +
  geom_bar(aes(x=.value, y=..prop.., fill=.group, group=.group), position="dodge")

# .value is character
ggplot(dat %>% mutate(.value=as.character(.value))) +
  geom_bar(aes(x=.value, y=..prop.., fill=.group), position="dodge")

ggplot(dat %>% mutate(.value=as.character(.value))) +
  geom_bar(aes(x=.value, y=..prop.., fill=.group, group=.group), position="dodge")
当x变量是分类变量时,

geom_line的行为类似。例如:

set.seed(3)
dat = data.frame(x=rep(paste0(2010:2015, "-", substr(2011:2016,3,4)), 2),
                 y=cumsum(rnorm(12)),
                 group=rep(LETTERS[1:2], each=6))

ggplot(dat, aes(x=x, y=y, colour=group)) +
  geom_point() +
  geom_line()

ggplot(dat, aes(x=x, y=y, colour=group, group=group)) +
  geom_point() +
  geom_line()