geom_line不使用fun.y = mean作为参数生成行

时间:2014-05-23 02:48:44

标签: r ggplot2

这是我的数据:

head(BCM)
        fips      scc pollutant emissions  type year
114288 24510 10100601  PM25-PRI     6.532 POINT 1999
114296 24510 10200601  PM25-PRI    78.880 POINT 1999
114300 24510 10200602  PM25-PRI     0.920 POINT 1999
114308 24510 30100699  PM25-PRI    10.376 POINT 1999
114325 24510 30183001  PM25-PRI    10.859 POINT 1999
114329 24510 30201599  PM25-PRI    83.025 POINT 1999

table(BCM$type)
NON-ROAD NONPOINT  ON-ROAD    POINT 
 416      142     1119      419 

table(BCM$year)
1999 2002 2005 2008 
320  535  542  699 

我想看看每种类型的污染物随时间变化的情况。这是我的代码:

ggplot(aes(x = year, y = emissions), data = BCM) +
geom_point(stat = "summary", fun.y = mean) +
facet_wrap(~type, scale = "free")

with geim_point

我真正想做的是将点连接起来形成线条。所以我认为这会奏效:

ggplot(aes(x = year, y = emissions), data = BCM) +
geom_line(stat = "summary", fun.y = mean) +
facet_wrap(~type, scale = "free")

但我得到的是许多警告线和一个没有点或线的情节:

geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
geom_path: Each group consist of only one observation. Do you need to adjust the group     aesthetic?
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

with geom_line

我对 R 相对较新。那么请有人告诉我出了什么问题?

1 个答案:

答案 0 :(得分:1)

如何翻转它

#sample data
gg<-expand.grid(type=letters[1:4], year=2000:2004)
df<-data.frame(
    gg,
    emissions=runif(nrow(gg)*10)
)

ggplot(aes(x=year, y=emissions), data=df) +
   stat_summary(geom="line", fun.y="mean") +
   facet_wrap(~type, scale="free")

stat summary line