【问题标题】:How to smoothen a line chart plot?如何平滑折线图?
【发布时间】:2021-02-10 16:00:51
【问题描述】:

我想平滑我的折线图并有以下代码:


ggplot7 <- ggplot(rye) +  geom_line(aes(x=date,y=ARIS_TOP), group=1, color='blue', alpha=0.5) +
                          geom_line(aes(x=date,y=SWI_001), group=2, color='darkturquoise',  alpha=0.5) +
                          scale_y_continuous(limits = c(7, 27, by = 2 )) + 
                          scale_x_date(labels = date_format('%b'), date_breaks = '2 month') +
                          ylab('Soil Moisture (%)')+xlab('Date') +
                          labs(title=('Absolute Soil Moisture')) + 
                          stat_smooth() +
                          geom_hline(yintercept= c(9, 23), size =0.75, color='firebrick', linetype= 'dashed')

ggplot7 + theme_bw(base_family='Playfair', base_size = 15, base_rect_size = 1) 

在线我找到了一些方法,但似乎我无法找到合适的代码。 这是我收到的错误消息:

`geom_smooth()` using method = 'loess' and formula 'y ~ x'
Fehler: stat_smooth requires the following missing aesthetics: x and y
Run `rlang::last_error()` to see where the error occurred.

【问题讨论】:

  • 您能否将您正在使用的确切 geom_smooth() 调用添加到示例代码中?此外,该错误会通知您在您的geom_smooth() 调用中可能需要aes(x=date,y=ARIS_TOP)aes(x=date,y=SWI_001)
  • @teunbrand 我使用了这个代码:geom_smooth(method = 'gam', formula = y~x, span = 1.5, ) 我收到的消息如下:mapping: yintercept = ~yintercept geom_hline: na.rm = FALSE stat_identity: na.rm = FALSE position_identity

标签: r ggplot2 linechart


【解决方案1】:

正如 cmets 中所指出的,在您的 stat_smooth() 调用中使用 aes(x=date,y=ARIS_TOP)aes(x=date,y=SWI_001)

stat_smooth(aes(x=date,y=ARIS_TOP)) +

stat_smooth(aes(x=date,y=SWI_001)) +

或连续两个命令。

【讨论】:

    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多