1

I have data In three flavours and want to show it as three different plots in a chart. I also want to use geom_smooth on the plots. When one of the flavours of data has less than seven values, geom_smooth can not be rendered for that plot. This is fine, since it is rendered for the other plots. (second example) However, I noticed that when one of the flavours has two or less values, geom_smooth is not rendered for any of the plots in the chart. I would like to use geom_smooth on the other two flavours even though one flavour has below two data points.

I find the warnings/errors hard to interpret since they change depending on how many data points the last flavour has.

Example with two data points

library(ggplot2)

dat <- data.frame("Year" = c(1:10, 1:10, 4,5),
                  "Val" = c(46,57,36,58,36,57,36,56,46,58,
                          23,28,26,38,26,37,26,26,36,38,
                          3,5),
                  "Flavour" = c(rep("type A",10), rep("type B", 10), rep("type C",2)))

ggplot(dat, aes(x = Year,
                y = Val,
                color = Flavour,
                group = Flavour)) +
  geom_smooth(alpha = 0.5) +
  geom_line() +
  geom_point()

enter image description here

Example with more than two data points

library(ggplot2)

dat <- data.frame("Year" = c(1:10, 1:10, 1:3),
                  "Val" = c(46,57,36,58,36,57,36,56,46,58,
                        23,28,26,38,26,37,26,26,36,38,
                        3,5,5),
                  "Flavour" = c(rep("type A",10), rep("type B", 10), rep("type C",3)))

ggplot(dat, aes(x = Year,
                y = Val,
                color = Flavour,
                group = Flavour)) +
  geom_smooth(alpha = 0.5) +
  geom_line() +
  geom_point()

enter image description here

chilifan
  • 157
  • 1
  • 12
  • 1
    This question is related: [https://stackoverflow.com/questions/52695969/ggplot-fails-to-draw-smooth-gam-using-facet-wrap-and-group-asthetic](https://stackoverflow.com/questions/52695969/ggplot-fails-to-draw-smooth-gam-using-facet-wrap-and-group-asthetic). It has a somewhat exhaustive analysis of the problem and shows some possible ways around it... – dario Oct 29 '21 at 13:35

0 Answers0