C.Tu
C.Tu

Reputation: 13

Smoothing Lines in ggplot between all data point

I have a data.frame similar to this example

SqMt <- "Sex Sq..Meters PDXTotalFreqStpy 
    1          M        129          22         
    2          M        129          0         
    3          M        129          1         
    4          F        129          35         
    5          F        129          42         
    6          F        129          5         
    7          M        557          20         
    8          M        557          0         
    9          M        557          15         
    10         F        557          39         
    11         F        557          0         
    12         F        557          0         
    13         M        1208         33         
    14         M        1208         26         
    15         M        1208         3         
    16         F        1208         7         
    17         F        1208         0         
    18         F        1208         8         
    19         M        604          68         
    20         M        604          0         
    21         M        604          0         
    22         F        604          0         
    23         F        604          0         
    24         F        604          0"

Data <- read.table(text=SqMt, header = TRUE)

I want to show the average PDXTotalFreqStpy for each Sq..Meters organized by Sex. This is what I use:

library(ggplot2)
ggplot(Data, aes(x=Sq..Meters, y=PDXTotalFreqStpy)) + stat_summary(fun.y="mean", geom="line", aes(group=Sex,color=Sex))

How do I get these lines smoothed out so that they are not jagged and instead, nice and curvy and go through all the data points? I have seen things on spline, but I have not gotten those to work?

Upvotes: 0

Views: 367

Answers (1)

Z.Lin
Z.Lin

Reputation: 29075

See if this works for you:

library(dplyr)

# increase n if the result is not smooth enough
# (for this example, n = 50 looks sufficient to me)
n = 50

# manipulate data to calculate the mean for each sex at each x-value
# before passing the result to ggplot()
Data %>%
  group_by(Sex, x = Sq..Meters) %>%
  summarise(y = mean(PDXTotalFreqStpy)) %>%
  ungroup() %>%

  ggplot(aes(x, y, color = Sex)) +

  # optional: show point locations for reference
  geom_point() +

  # optional: show original lines for reference
  geom_line(linetype = "dashed", alpha = 0.5) +

  # further data manipulation to calculate values for smoothed spline
  geom_line(data = . %>%
              group_by(Sex) %>%
              summarise(x1 = list(spline(x, y, n)[["x"]]),
                        y1 = list(spline(x, y, n)[["y"]])) %>%
              tidyr::unnest(),
            aes(x = x1, y = y1))

plot

Upvotes: 2

Related Questions