fill 参数使 geom_label() 中的位置参数无效

Joe*_*Joe 5 r ggplot2

当我在 geom_label 中使用填充参数时,我的位置参数无效。geom_text() 不会发生这种情况。如何将填充和位置参数与 geom_label() 一起使用?

库(dplyr) 库(ggplot2)

ds_mt <- 
  mtcars %>% 
  group_by(cyl, am) %>% 
  summarise(mpg = mean(mpg)) %>% 
  ungroup() %>% 
  mutate_at(vars(cyl, am), funs(as.factor)) 

ds_mt %>% 
  ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
  geom_col(position = position_dodge()) + 
  geom_label(position = position_dodge(width = .9))

# Adding fill argument to geom_label() nullifies the position argument
ds_mt %>% 
  ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
  geom_col(position = position_dodge()) + 
  geom_label(fill = "white", position = position_dodge(width = .9))

ds_mt %>% 
  ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
  geom_col(position = position_dodge()) + 
  geom_label(position = position_dodge(width = .9), fill = "white")

# no problems with geom_text
ds_mt %>% 
  ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
  geom_col(position = position_dodge()) + 
  geom_text(position = position_dodge(width = .9), color = "blue")
Run Code Online (Sandbox Code Playgroud)

小智 6

您需要在您的geom_label().

ds_mt %>% 
  ggplot(aes(x = cyl, fill = as.factor(am), y = mpg, label = am)) + 
  geom_col(position = position_dodge()) + 
  geom_label(aes(group = factor(am)), fill = "white", position = position_dodge(width = .9))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述