【问题标题】:ggplot2 add line of loess when using `stat_summary`ggplot2在使用`stat_summary`时添加黄土线
【发布时间】:2017-11-12 11:31:27
【问题描述】:

我正在尝试为一些数据创建一条黄土线

这里是一些代码:

library(ggplot2)

#rm(list=ls())
#gc()
#.rs.restartR()
###############################################################################

## Create some numbers for testing
m = 200
set.seed(123)
Aboard <- sample(1:m,m)
## some years to use
Years <- sort( trunc( runif( m, min=1931, max=1990 ) ) )

df <- data.frame(Aboard, Years)

graph <- ggplot(df, aes(Years, Aboard))

graph <- graph + stat_summary(fun.y=sum, geom="point", aes(size=..y..))
graph <- graph + theme_bw()
graph <- graph + stat_summary(fun.y=sum, geom="smooth", method="loess", alpha=0.01)

graph <- graph + theme(text = element_text(size=16))
graph <- graph + labs(title = "Some information that is here, from a computer,\nJune 2004")
graph <- graph + theme(plot.title = element_text(hjust=0.5))
graph <- graph + theme(plot.title = element_text(size=20))
graph <- graph + labs(x = "Year") + labs(y = "Information")
graph <- graph + stat_summary(fun.y=sum, geom="line")

graph

这是上面的输出:

我期待这条线

graph <- graph + stat_summary(fun.y=sum, geom="smooth", method="loess", alpha=0.01)

创建黄土线,但它只是适合数据点。

编辑

如果可能的话,我想要一个没有 tidyverse / dplyr 的解决方案,因为我没有使用这些

【问题讨论】:

    标签: r ggplot2 statistics


    【解决方案1】:
    library(ggplot2)
    m = 200
    set.seed(123)
    Aboard <- sample(1:m,m)
    Years <- sort( trunc( runif( m, min=1931, max=1990 ) ) )
    df <- data.frame(Aboard, Years)
    
    # Add a column with sums by years    
    library(dplyr)
    df <- df %>% group_by(Years) %>% mutate(ysum=sum(Aboard))
    
    graph <- ggplot(df, aes(Years, Aboard))
    graph <- graph + stat_summary(fun.y=sum, geom="point", aes(size=..y..))
    graph <- graph + theme_bw()
    
    # Use geom_smooth in place of stat_summary
    graph <- graph + geom_smooth(aes(y=ysum), alpha=0.5)
    
    graph <- graph + theme(text = element_text(size=16))
    graph <- graph + labs(title = "Some information that is here, from a computer,\nJune 2004")
    graph <- graph + theme(plot.title = element_text(hjust=0.5))
    graph <- graph + theme(plot.title = element_text(size=20))
    graph <- graph + labs(x = "Year") + labs(y = "Information")
    graph <- graph + stat_summary(fun.y=sum, geom="line")
    graph
    

    如果您需要避免使用dplyr 包:

    m = 200
    set.seed(123)
    Aboard <- sample(1:m,m)
    Years <- sort( trunc( runif( m, min=1931, max=1990 ) ) )
    df <- data.frame(Aboard, Years)
    
    # Sums by years    
    df2 <- aggregate(x=df$Aboard, list(df$Years), FUN=sum)
    names(df2) <- c("Years","ysum")
    
    graph <- ggplot(df, aes(Years, Aboard))
    graph <- graph + stat_summary(fun.y=sum, geom="point", aes(size=..y..))
    graph <- graph + theme_bw()
    # Use geom_smooth in place of stat_summary
    graph <- graph + geom_smooth(data=df2, aes(x=Years, y=ysum), alpha=0.5)
    
    graph <- graph + theme(text = element_text(size=16))
    graph <- graph + labs(title = "Some information that is here, from a computer,\nJune 2004")
    graph <- graph + theme(plot.title = element_text(hjust=0.5))
    graph <- graph + theme(plot.title = element_text(size=20))
    graph <- graph + labs(x = "Year") + labs(y = "Information")
    graph <- graph + stat_summary(fun.y=sum, geom="line")
    graph
    

    【讨论】:

    • 这个需要 dplyr 吗?因为我没有使用它,如果可能的话,没有它的解决方案会很好
    猜你喜欢
    • 2021-10-16
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    相关资源
    最近更新 更多