在R中绘制每个级别或类别的图表

时间:2018-03-29 04:50:14

标签: r plot categories factors levels

我有一个数据集,其中包含按国家/地区和年份列出的儿童死亡人数(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  

附上我想要的输出图片。output desired

1 个答案:

答案 0 :(得分:0)

你走了:

> 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

您可以使用facet_wrap中的ggplot2执行您要查找的内容:

> df %>% group_by(country) %>% 
         ggplot(aes(x=year,y=sab)) + geom_point() + 
         facet_wrap(~country)

它产生了这个:

enter image description here

对于具有100+分类变量值的数据集,我实际上没有这样做,因此不确定它将如何缩放。网上有很多资源可以帮助你调整ggplot2参数......只要记住它就像Photoshop ......你总是要分层思考。希望这有帮助!