0

I rerun the code from this link with the version of ggplot2==3.3.5:

df <- structure(list(date = c("2022-2-1", "2022-2-2", "2022-2-3", "2022-2-4", 
"2022-2-5", "2022-2-6", "2022-2-7", "2022-2-8", "2022-2-9", "2022-2-10", 
"2022-2-11", "2022-2-12", "2022-2-13", "2022-2-14", "2022-2-15", 
"2022-2-16", "2022-2-17"), pct_change = c(4, 4, 4.04, 4.04, 4.04, 
4.44, 4.88, 4.62, 4.8, 5.2, 4.7, 5.06, 4.56, 4.8, 4.32, 4.02, 
4.01)), class = "data.frame", row.names = c(NA, -17L))

df1 <- df %>% 
  mutate_at(vars(-date), funs(./100))

df1 %>% 
  ggplot(aes(x=as.POSIXct(date), y=pct_change)) + 
  stat_smooth(
    geom = "area", 
    size = 1,
    fill = "red", 
    color = "red", 
    alpha = 0.3, 
    span = .3
  ) +
  coord_cartesian(ylim = c(.04, .0525), expand = FALSE)

It unexpectedly generates the warning messages:

geom_smooth() using method = 'loess' and formula 'y ~ x' Warning in stats::qt(level/2 + 0.5, pred$df) : NaNs produced

enter image description here

which leads to the area didn't get filled with red color as @zephryl's ouput shown:

enter image description here

How could we solve this issue? Thanks for your help at advance.

Edit 1:

I copy the chuck of code above and run reprex::reprex():

> library(ggplot2)
> library(tidyverse)
> 
> df <- structure(list(date = c("2022-2-1", "2022-2-2", "2022-2-3", "2022-2-4", 
+                               "2022-2-5", "2022-2-6", "2022-2-7", "2022-2-8", "2022-2-9", "2022-2-10", 
+                               "2022-2-11", "2022-2-12", "2022-2-13", "2022-2-14", "2022-2-15", 
+                               "2022-2-16", "2022-2-17"), pct_change = c(4, 4, 4.04, 4.04, 4.04, 
+                                                                         4.44, 4.88, 4.62, 4.8, 5.2, 4.7, 5.06, 4.56, 4.8, 4.32, 4.02, 
+                                                                         4.01)), class = "data.frame", row.names = c(NA, -17L))
> 
> df1 <- df %>% 
+   mutate_at(vars(-date), funs(./100))
> 
> df1 %>% 
+   ggplot(aes(x=as.POSIXct(date), y=pct_change)) + 
+   stat_smooth(
+     geom = "area", 
+     size = 1,
+     fill = "red", 
+     color = "red", 
+     alpha = 0.3, 
+     span = .3
+   ) +
+   coord_cartesian(ylim = c(.04, .0525), expand = FALSE)
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Warning message:
In stats::qt(level/2 + 0.5, pred$df) : NaNs produced
> 
> reprex::reprex(style = TRUE)
i Rendering reprex...
√ Reprex output is on the clipboard.

Output:

This reprex appears to crash R. See standard output and standard error for more details.

Standard output and error

Quitting from lines 23-48 (grave-cobra_reprex.spin.Rmd) 
Error in curl::curl_fetch_memory(url, handle = handle) : 
  schannel: failed to receive handshake, SSL/TLS connection failed
ah bon
  • 9,293
  • 12
  • 65
  • 148
  • Weird... while I get the warnings as well, I do get the filled version as output (dplyr_1.0.8 and ggplot2_3.3.5 on either ubuntu or macOS). – user12728748 Mar 08 '22 at 10:27
  • Yeah, I get this result by using `dplyr==1.0.7` and `ggplot2==3.3.5` under Windows 10, I'll try with Mac later. – ah bon Mar 08 '22 at 10:37
  • Try this https://stackoverflow.com/questions/49502917/trendline-cannot-be-displayed-after-putting-observer-in-my-shiny-app – Rfanatic Mar 08 '22 at 10:39
  • Sorry, I didn't get it, how should I modify the code according to this link, replace `stat_smooth` with `geom_smooth`? – ah bon Mar 08 '22 at 10:42
  • I tested in Mac system, I get the filled version of output also. But I don't why it's not the case in Win10. – ah bon Mar 08 '22 at 12:11
  • can you run this on your windows 10 with the [reprex package](https://reprex.tidyverse.org), please? This might help clarify if this is a OS problem, or related to weird package/ object interferences. – tjebo Mar 08 '22 at 13:32
  • Not sure if I've done that correctly, please see edit 1 in the end of the question. @tjebo – ah bon Mar 08 '22 at 14:56
  • easiest to use the reprex add on in RStudio. If you don't use RStudio, check the examples in `?reprex::reprex` how to correctly render code. Occasionally I am calling this function directly (e.g., when I want to change chunk settings, i.e., in most cases figure dimensions) usually copy paste the example and include my code at the right place - all pseudo comments with hash etc and quotation marks are important, so don't delete them) – tjebo Mar 09 '22 at 15:06
  • But I've tried it out now and you're right, you should be able to call reprex::reprex just after copying to the clipboard. didn't know this, that's kind of nice. I'll show my output on a OSX, as a pseudo-answer – tjebo Mar 09 '22 at 15:10

1 Answers1

1

Not an answer, just too much code for a comment. On my Mac, this is not reproducible. As per comment, ideally try to use the reprex package in order to check if this is really related to your OS.

Below the code used for my reprex. If your reprex call should crash the session, please try on a different machine, if you're using reprex correctly. Sometimes reprex just doesn't work, and keeps crashing, and you need to restart R / RStudio in order to make it work again.

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df <- structure(list(date = c("2022-2-1", "2022-2-2", "2022-2-3", "2022-2-4", 
                              "2022-2-5", "2022-2-6", "2022-2-7", "2022-2-8", "2022-2-9", "2022-2-10", 
                              "2022-2-11", "2022-2-12", "2022-2-13", "2022-2-14", "2022-2-15", 
                              "2022-2-16", "2022-2-17"), pct_change = c(4, 4, 4.04, 4.04, 4.04, 
                                                                        4.44, 4.88, 4.62, 4.8, 5.2, 4.7, 5.06, 4.56, 4.8, 4.32, 4.02, 
                                                                        4.01)), class = "data.frame", row.names = c(NA, -17L))

df1 <- df %>% 
  mutate_at(vars(-date), funs(./100))
#> Warning: `funs()` was deprecated in dplyr 0.8.0.
#> Please use a list of either functions or lambdas: 
#> 
#>   # Simple named list: 
#>   list(mean = mean, median = median)
#> 
#>   # Auto named with `tibble::lst()`: 
#>   tibble::lst(mean, median)
#> 
#>   # Using lambdas
#>   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.

df1 %>% 
  ggplot(aes(x=as.POSIXct(date), y=pct_change)) + 
  stat_smooth(
    geom = "area", 
    size = 1,
    fill = "red", 
    color = "red", 
    alpha = 0.3, 
    span = .3
  ) +
  coord_cartesian(ylim = c(.04, .0525), expand = FALSE)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> Warning in stats::qt(level/2 + 0.5, pred$df): NaNs produced

Created on 2022-03-09 by the reprex package (v2.0.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94