Saya mencoba membuat garis loess untuk beberapa data

Berikut adalah beberapa kode:

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

Berikut adalah output dari di atas:

enter image description here

Saya mengharapkan baris ini

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

Untuk membuat garis loess, tetapi hanya pas dengan titik data.

Sunting

Jika memungkinkan saya ingin solusi tanpa rapi/dplyr karena saya tidak menggunakannya

0
baxx 12 November 2017, 14:31

1 menjawab

Jawaban Terbaik
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

Jika Anda perlu menghindari paket 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

enter image description here

3
marc_s 4 Mei 2019, 10:19