geom_rect not working with scale_y_log10() (ggplot

2019-08-15 08:34发布

ggplot(economics) + 
geom_rect(
  aes(
    xmin = economics$date[20], 
    xmax = economics$date[140], 
    ymin = -Inf, 
    ymax = Inf
  ), 
  fill = "grey", 
  color = "grey", 
  alpha = 0.4
) + 
geom_line(aes(x = date, y = pce))

works fine and produces the desired plot. But if one were to transform the y-axis to log10 (by using scale_y_log10())

ggplot(economics) + 
geom_rect(
  aes(
    xmin = economics$date[20], 
    xmax = economics$date[140], 
    ymin = -Inf, 
    ymax = Inf
  ), 
  fill = "grey", 
  color = "grey", 
  alpha = 0.4
) + 
geom_line(aes(x = date, y = pce)) + 
scale_y_log10()

it gives the following error:

Warning messages:
1: In self$trans$transform(x) : NaNs produced
2: Removed 574 rows containing missing values (geom_rect).

and removes the geom_rect layer.

标签: r ggplot2
0条回答
登录 后发表回答