0

I am trying to plot a smoothed trendline but for various reasons I cannot use geom_smooth() to achieve my goal.

The data is in the format:

time value Group
1 124 1
1 452 2
2 49 1
2 453 2
3 788 1
3 652 2
4 356 1
4 452 2

The code I have so far is:

ggplot(data, 
       aes(x = time,
           y = value,
           color = Group)) +
  geom_point() +
  geom_xspline() +
  theme(line = element_line(linewidth = 1))

This does not change the width of the geom_xspline(). Neither does geom_xspline(size = 2) or

ggplot(data, 
       aes(x = time,
           y = value,
           color = Group,
           size = 2)) +
  geom_point() +
  geom_xspline()
swediot
  • 19
  • 4
  • 1
    `geom_xspline(size = 2)` works fine for me. Perhaps you have to convert `Group` to a factor, i.e. try with `color = factor(Group)` too. BTW: The `theme()` does not affect the appearance of geoms, just the non-data-ink like the width of the axis line. And if you want to set the `size` (or `color` or ... ) then do this as an argument, i.e. outside of `aes()` in the geom. – stefan Aug 29 '23 at 08:13

2 Answers2

0

To use the geom_smooth method:

ggplot(data , aes(x = time, y = value ,color = as.factor(Group)))  + 
 geom_point() + 
 geom_smooth(stat = "smooth", method = "lm", se = FALSE, lwd=1, formula = 
 'y~x')

Note: As @swediot above mentioned, use as.factor(Group)

For geom_smooth , select a method, 'lm' or 'loess' , se = True or false for confidence interval.

lwd = 1 for linewidth , and then the formula that you want the line to use.

Check out geom_smooth on here: https://ggplot2.tidyverse.org/reference/geom_smooth.html

  • 1
    Hi, unfortunately I can't use geom_smooth since I need to force the fitted line to go through all of the datapoints in the dataset. – swediot Aug 29 '23 at 10:11
  • library(ggalt) ggplot(data , aes(x = time, y = value ,color = as.factor(group))) + geom_point() + geom_xspline() + aes(lwd = 2) – Nic Coxen Aug 29 '23 at 11:07
  • Change aes(lwd = 2) depending on your line width – Nic Coxen Aug 29 '23 at 11:07
0
library(ggalt)

ggplot(data , aes(x = time, y = value ,color = as.factor(group)))  + 
geom_point() + 
geom_xspline() + 
aes(lwd = 2)

Change aes(lwd = 2) depending on your line width

Hope this helps