簡體   English   中英

ggplot2使用`stat_summary`時添加黃土線

[英]ggplot2 add line of loess when using `stat_summary`

我正在嘗試為一些數據創建一條黃土線

這是一些代碼:

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的解決方案,因為我不使用那些

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM