facet_wrap(scales)和scale_y_continous(标签,中断)之间的冲突

时间:2019-11-19 10:37:39

标签: r ggplot2

让我们有一个数据框...

library(tidyverse)

dates <- c("2019-03-01", "2019-04-01", "2019-05-01", "2019-06-01", "2019-07-01")
d <- data_frame(month = dates %>% factor(ordered = TRUE),
                subject = "Abc_14",
                taux = runif(length(dates)),
                volume = (runif(length(dates)) * 100) %>% as.integer())
d %>% glimpse()
Observations: 5
Variables: 4
$ month   <ord> 2019-03-01, 2019-04-01, 2019-05-01, 2019-06-01, 2019-07-01
$ subject <chr> "Abc_14", "Abc_14", "Abc_14", "Abc_14", "Abc_14"
$ taux    <dbl> 0.67040271, 0.85711442, 0.94608828, 0.65138723, 0.02835217
$ volume  <int> 52, 46, 33, 17, 69

然后让这个失败的图出现:

d %>% 
  ggplot(aes(volume, taux)) +
  geom_density2d(size = .2) +
  geom_point(aes(fill = subject)) +
  geom_smooth(method = "lm", se = FALSE) +

  facet_wrap(facets = vars(month), scales = "free") +

  scale_y_continuous(labels = scales::percent, breaks = seq(0, 1, .2)) +

  theme(legend.position = "none")
Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : argument 'nsmall' incorrect

似乎facet_wrapscale_y_continuous之间存在冲突。排除其中任何一个,我都会得到结果。 我有什么办法让他们俩都保留?

没有scale_y_continous(),我明白了,但是我确实需要百分比标签: enter image description here

注意:实际数据框有大约200行,大约30个主题,大约10个月,并且缺少一些组合。它仍在增长。

1 个答案:

答案 0 :(得分:1)

您必须用

之类的内容替换scale_y_continuous(labels = scales::percent, breaks = seq(0, 1, .2))
scale_y_continuous(labels = scales::percent_format(accuracy=2), breaks = seq(0, 1, .2))