首页 > 解决方案 > 试图在 R 中的 ggplot 图上获得更平滑的线

问题描述

所以我有一个折线图,我想让它看起来比现在更平滑一些。现在,图表上有一堆锯齿状的边缘,我希望它弯曲一点。我尝试使用 geom_smooth ,但这使得最大的 y 值接近 150,而它应该接近 230(如图所示)。知道如何使这个折线图更平滑一点吗?

library(tidyverse)
library(ggplot2)
library(ggrepel)
library(dplyr)

source("https://raw.githubusercontent.com/samhoppen/2020_FF_Analysis/master/Functions/fpros_scrape_projections.R")
source("https://raw.githubusercontent.com/samhoppen/2020_FF_Analysis/master/Functions/set_vor.R")

position <- c("qb", "rb", "wr", "te")
week <- "draft"

crossing(position, week)

projections <- crossing(position = position, week = week) %>% 
  pmap_dfr(scrape_projections) %>%
  mutate_at(vars(pass_yds, rush_yds, rec_yds), str_remove, ",") %>% 
  mutate_at(vars(pass_att:rec_tds), as.numeric) %>%
  mutate_at(vars(team:position), as.factor) %>% 
  select(-c("week")) %>% 
  as_tibble()

default_baseline <- c(QB = 15, RB =72, WR = 90, TE = 30, K = 6)

vor_table <- set_vor(projections,
                             vor_baseline = default_baseline,
                             vor_var = "fpts")
ggplot() +
  geom_line(data = vor_table,
              aes(x = fpts_rank, y = fpts_vor))

标签: rggplot2graphcharts

解决方案


这可能有效:

ggplot()+  
stat_smooth(data = vor_table, aes(x = fpts_rank, y = fpts_vor), 
            se = F, method = "lm", formula = y ~ poly(x, 10))

推荐阅读