如何在R中绘制年龄段的堆积线图?

时间:2019-05-10 14:40:35

标签: plot graph percentage

我正在尝试绘制一个折线图,显示一个城市在所有年份(2005-2012年)内的年龄分布(less1-less80)。因此,每个市镇ID均会显示所有年份的所有百分比。后来,我想将此图与另一个显示同一市镇和同一年死亡率的图合并。我的目标是在年龄分布和死亡率之间进行直接比较,以查看人口老龄化的城市是否有较高的死亡率。

我到处寻找诸如R stacked line graph with ggplot之类的解决方案 和How to plot stack series line graph in R? 但是,我找不到任何解决方案来帮助我处理数据。

示例如下:

municipality_id     Date variable  value
1               110001 Jan 2005    less1  1.94%
2               110037 Jan 2005    less1  2.45%
3               110040 Jan 2005    less1  2.32%
4               110034 Jan 2005    less1  1.97%
5               110002 Jan 2005    less1  2.39%
6               110045 Jan 2005    less1  2.55%
7               110003 Jan 2005    less1  2.03%
489674          431475 Jan 2012   less80 96.29%
489675          431514 Jan 2012   less80 97.61%
489676          432055 Jan 2012   less80 97.71%


==X==============================================================X==
     Copy+Paste this part. (If on a Mac, it is already copied!)
==X==============================================================X==

 b4 <- structure(list(municipality_id = structure(c(1L, 26L, 27L, 25L,2L, 28L, 3L, 4924L, 4934L, 5033L), .Label = c("110001", "110002","110003", "110004", "110005", "110006", "110007", "110008", "110009","110010", "110011", "110012", "110013", "110014", "110015", "110018","110020", "110025", "110026", "110028", "110029", "110030", "110032","110033", "110034", "110037", "110040", "110045", "220100","220105", "220110", "220115", "220117", "220120", "220130", "220140","220150", "220155", "220157", "220160", "220170", "220173", "220177","220180", "220190", "220191", "220192", "220194", "220196", "220198","220200", "220202", "220205", "220207", "220208", "220209", "220210","220211", "220213", "220217", "220220", "220225", "220230", "240060","240070", "240080", "240090", "240100", "240110", "240120", "240130","240140", "240145", "240150", "240160", "240165", "240170", "240180","240185", "240190", "240200", "240210", "240220", "240230",  "270760","270770", "270780", "270790", "270800", "270810", "270820", "270830","270840", "270850", "270860", "270870", "270880", "270890", "270895","270900", "270910", "270915", "270920", "270930", "270940", "280010","280020", "280030", "280040", "280050", "280060", "280067", "280070","280100", "280110", "280120", "280130", "280140", "280150", "280160","280170", "280190", "280200", "280210", "280220", "280230", "280240","280250", "280260", "280270", "280280", "280290", "280300", "280310","280320", "280330", "280340", "280350", "280360", "280370", "280380","280390", "280400", "280410", "280420", "280430", "280440", "280445","280450", "280460", "280470", "280480", "280490", "280500",  "315810","315820", "315830", "315840", "315850", "315860", "315870", "315880","315890", "315895", "315900", "315910", "315920", "315930", "315935","315940", "315950", "315960", "315970", "315980", "315990", "316000","316010", "316020", class = "factor"),Date = structure(c(2005, 2005, 2005, 2005, 2005, 2005, 2005,2012, 2012, 2012), class = "yearmon"), variable = structure(c(1L,1L, 1L, 1L, 1L, 1L, 1L, 11L, 11L, 11L), .Label = c("less1","less5", "less10", "less15", "less20", "less30", "less40","less50", "less60", "less70", "less80"), class = "factor"),value = c("1.94%", "2.45%", "2.32%", "1.97%", "2.39%", "2.55%","2.03%", "96.29%", "97.61%", "97.71%")), row.names = c(1L,2L, 3L, 4L, 5L, 6L, 7L, 489674L, 489675L, 489676L), class = "data.frame") 

==X==============================================================X==

我的总data.frame具有以下功能:

'data.frame':   489676 obs. of  4 variables:
 $ municipality_id: int  110001 110037 110040 110034 110002 110045 110003         110060 110004 110070 ...
 $ Date           : 'yearmon' num  Jan 2005 Jan 2005 Jan 2005 Jan 2005 ...
 $ variable       : Factor w/ 11 levels "less1","less5",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value          : chr  "1.94%" "2.45%" "2.32%" "1.97%" ...

我尝试过

ggplot(b4, aes(x=municipality_id, y=value, col=variable, group=municipality_id)) + geom_line()

ggplot(data = b4, mapping = aes(x = municipality_id)) + geom_freqpoly(mapping = aes(color = variable), binwidth = 10)

xyplot(value ~ municipality_id, data=b4, type='b', groups = municipality_id)

我想在这里使我的图形看起来像正确的图像:https://www.google.com/search?q=plot+age+distribution+in+r&tbm=isch&source=univ&client=firefox-b-d&sa=X&ved=2ahUKEwj04qbshJHiAhXR5KQKHfeGB48Q7Al6BAgJEA8&biw=1354&bih=686#imgrc=Gmn8-rHwkiLEQM

其中

年是Municipality_id 丰富就是价值 物种是可变的(范围从less1到less80)

0 个答案:

没有答案