在 R 中为每个级别或类别绘图
Plotting in R for each level or category
我有一个数据集,其中包含按国家和年份列出的儿童死亡人数 (cdeaths) 的数据。如何在无需手动操作的情况下为每个国家/地区生成单独的散点图(按年计算的死亡人数)?我有 186 个国家/地区,所以我想知道是否有一种方法可以按因数进行计算。
数据集是这样的
country year cdeaths
Afghanistan 2015 50.5
Afghanistan 2014 45.2
Afghanistan 2011 39.9
Afghanistan 2011 38.6
Afghanistan 2010 34.3
Afghanistan 2008 24
Afghanistan 2006 19
Afghanistan 2003 14.3
Afghanistan 2002 15
Afghanistan 1999 15
Barbados 2015 99
Barbados 2014 99.7
Barbados 2013 98.6
Barbados 2012 98.9
Barbados 2012 100
Barbados 2011 100
Barbados 2008 100
Barbados 2007 100
Barbados 2006 100
Barbados 2005 100
Barbados 2004 100
Barbados 2003 100
Barbados 2002 100
Barbados 2000 98
Barbados 1999 91
Barbados 1995 100
Cambodia 2014 89
Cambodia 2011 71.7
Cambodia 2010 71
Cambodia 2009 63
Cambodia 2008 58
Cambodia 2005 43.8
Cambodia 2004 16.3
Cambodia 2000 31.8
Cambodia 1998 34
Cambodia 1995 43.3
Denmark 2016 94.4
Denmark 2015 95.4
Denmark 2014 95.9
Denmark 2013 96.3
Denmark 2012 98.2
Denmark 2011 98.5
Denmark 2010 98.5
Denmark 2009 98.7
Denmark 2007 97.8
Denmark 2006 98.7
Denmark 2005 98.8
Denmark 2004 98.6
Denmark 2003 98.9
Denmark 2002 98.8
Denmark 2001 98.9
Denmark 2000 98.8
Denmark 1999 98.7
Denmark 1998 98.8
Denmark 1997 98.3
Estonia 2016 99.4
Estonia 2015 99.5
Estonia 2014 99.4
Estonia 2013 99.4
Estonia 2012 99.3
Estonia 2011 99.4
Estonia 2010 99.3
Estonia 2009 99.2
Estonia 2008 99.3
Estonia 2007 99.4
Estonia 2006 99.5
Estonia 2006 99.8
Estonia 2005 99.6
Estonia 2005 99.8
Estonia 2004 99.7
Estonia 2004 99.8
Estonia 2003 99.4
Estonia 2003 99.7
Estonia 2002 99.5
Estonia 2002 99.6
Estonia 2001 99.6
Estonia 2001 99.7
Estonia 2000 99.5
Estonia 2000 99.7
Estonia 1999 99.5
Estonia 1999 99.6
Estonia 1998 99.5
Estonia 1998 99.6
Estonia 1997 99.3
Estonia 1997 99.5
Estonia 1996 99.4
Estonia 1996 99.6
Estonia 1995 99.3
Estonia 1995 99.5
Estonia 1994 99.1
Estonia 1994 99.3
Estonia 1993 99.1
Estonia 1993 99.1
Estonia 1992 98.9
Estonia 1992 99
Gabon 2012 89.3
Gabon 2000 85.5
附上我想要的输出图片。
给你:
> library(dplyr)
> library(tibble)
> lisa <- read.table("D:/R/SO/lisa.txt",header=TRUE) # my path
> df <- as.tibble(lisa)
> df
# A tibble: 97 x 3
country year sab
<fct> <int> <dbl>
1 Afghanistan 2015 50.5
2 Afghanistan 2014 45.2
3 Afghanistan 2011 39.9
...etc
# ... with 87 more rows
您可以在 ggplot2
:
中使用 facet_wrap
执行您想要的操作
> df %>% group_by(country) %>%
ggplot(aes(x=year,y=sab)) + geom_point() +
facet_wrap(~country)
它产生这个:
我实际上还没有对具有 100 多个分类变量值的数据集执行此操作,因此不确定它将如何扩展。有很多在线资源可以帮助您调整 ggplot2
参数……请记住,它就像 Photoshop……您总是需要分层思考。希望这对您有所帮助!
我有一个数据集,其中包含按国家和年份列出的儿童死亡人数 (cdeaths) 的数据。如何在无需手动操作的情况下为每个国家/地区生成单独的散点图(按年计算的死亡人数)?我有 186 个国家/地区,所以我想知道是否有一种方法可以按因数进行计算。
数据集是这样的
country year cdeaths
Afghanistan 2015 50.5
Afghanistan 2014 45.2
Afghanistan 2011 39.9
Afghanistan 2011 38.6
Afghanistan 2010 34.3
Afghanistan 2008 24
Afghanistan 2006 19
Afghanistan 2003 14.3
Afghanistan 2002 15
Afghanistan 1999 15
Barbados 2015 99
Barbados 2014 99.7
Barbados 2013 98.6
Barbados 2012 98.9
Barbados 2012 100
Barbados 2011 100
Barbados 2008 100
Barbados 2007 100
Barbados 2006 100
Barbados 2005 100
Barbados 2004 100
Barbados 2003 100
Barbados 2002 100
Barbados 2000 98
Barbados 1999 91
Barbados 1995 100
Cambodia 2014 89
Cambodia 2011 71.7
Cambodia 2010 71
Cambodia 2009 63
Cambodia 2008 58
Cambodia 2005 43.8
Cambodia 2004 16.3
Cambodia 2000 31.8
Cambodia 1998 34
Cambodia 1995 43.3
Denmark 2016 94.4
Denmark 2015 95.4
Denmark 2014 95.9
Denmark 2013 96.3
Denmark 2012 98.2
Denmark 2011 98.5
Denmark 2010 98.5
Denmark 2009 98.7
Denmark 2007 97.8
Denmark 2006 98.7
Denmark 2005 98.8
Denmark 2004 98.6
Denmark 2003 98.9
Denmark 2002 98.8
Denmark 2001 98.9
Denmark 2000 98.8
Denmark 1999 98.7
Denmark 1998 98.8
Denmark 1997 98.3
Estonia 2016 99.4
Estonia 2015 99.5
Estonia 2014 99.4
Estonia 2013 99.4
Estonia 2012 99.3
Estonia 2011 99.4
Estonia 2010 99.3
Estonia 2009 99.2
Estonia 2008 99.3
Estonia 2007 99.4
Estonia 2006 99.5
Estonia 2006 99.8
Estonia 2005 99.6
Estonia 2005 99.8
Estonia 2004 99.7
Estonia 2004 99.8
Estonia 2003 99.4
Estonia 2003 99.7
Estonia 2002 99.5
Estonia 2002 99.6
Estonia 2001 99.6
Estonia 2001 99.7
Estonia 2000 99.5
Estonia 2000 99.7
Estonia 1999 99.5
Estonia 1999 99.6
Estonia 1998 99.5
Estonia 1998 99.6
Estonia 1997 99.3
Estonia 1997 99.5
Estonia 1996 99.4
Estonia 1996 99.6
Estonia 1995 99.3
Estonia 1995 99.5
Estonia 1994 99.1
Estonia 1994 99.3
Estonia 1993 99.1
Estonia 1993 99.1
Estonia 1992 98.9
Estonia 1992 99
Gabon 2012 89.3
Gabon 2000 85.5
附上我想要的输出图片。
给你:
> library(dplyr)
> library(tibble)
> lisa <- read.table("D:/R/SO/lisa.txt",header=TRUE) # my path
> df <- as.tibble(lisa)
> df
# A tibble: 97 x 3
country year sab
<fct> <int> <dbl>
1 Afghanistan 2015 50.5
2 Afghanistan 2014 45.2
3 Afghanistan 2011 39.9
...etc
# ... with 87 more rows
您可以在 ggplot2
:
facet_wrap
执行您想要的操作
> df %>% group_by(country) %>%
ggplot(aes(x=year,y=sab)) + geom_point() +
facet_wrap(~country)
它产生这个:
我实际上还没有对具有 100 多个分类变量值的数据集执行此操作,因此不确定它将如何扩展。有很多在线资源可以帮助您调整 ggplot2
参数……请记住,它就像 Photoshop……您总是需要分层思考。希望这对您有所帮助!