2016-12-22 62 views
0

我试图制作一个条形图,显示不同国家的人均国内生产总值,以便条形图上的条形根据这些国家的预期寿命具有不同的颜色。现在,我可以创建这样一个barplot,但只是离散的预期寿命值而不是一段时间。符合条件的不同颜色的条形图

如果国家的寿命从50岁到70岁不等,酒吧的颜色为黄色,70-80红色和80-90绿色,那么这将是完美的。

这里是我的代码:

data("focusgroup") 
par(mar = c(6,4,1,1)) 
x <- focusgroup[order(focusgroup$GDP), ] 
x$color[x$`LE Both Sexes`== 55] <- 1 
x$color[x$`LE Both Sexes`==77] <- 2 
x$color[x$`LE Both Sexes`==77] <- 3 
with(x, barplot(GDP, names.arg = x$Country, las = 2, cex.axis = 0.6, cex.lab = 0.8, cex = 0.6, col = color)) 

这里是焦点小组的数据样本: 结构(国家= C( “南非”, “斯威士兰”, “博”, “莱索托”,“纳米比亚“”瑞典“”挪威“”英国“”丹麦“ ”爱沙尼亚“”芬兰“”冰岛“”爱尔兰“”拉脱维亚“”立陶宛“ ”新西兰“ ,“澳大利亚”,“韩国”,“朝鲜”,“中国”, “日本”“蒙古”“墨西哥”“哥斯达黎加”“萨尔瓦多”“危地马拉” “洪都拉斯” ,“巴拿马”,“尼加拉瓜”),GDP = c(10700, 4500,14000,1700,6900,39100,54600,34800,36600,19100,35400, 38300,37300,14700,16000,27700,41000,3300,1800,7600, 34000,3600,13900,11300,7200 ,5200,4200,13000,3000),总体= c(54490,1287,2262,2135,2459,9779,5211,64716,5669,1313,5503, ,329,4688,1971,2878,4529,23969,50293 ,25155,1383925,126573, 2959,127017,4808,6127,16343,8075,3929,6082),LE Male = c(59.3, 56.6,63.3,51.7,63.1,80.7,79.8,79.4,78.6,72.7, 78.3,81.2, 79.4,69.6,68.1,80,80.9,78.8,67,74.6,80.5,64.7,73.9, 77.1,68.8,68.5,72.3,74.7,71.5),LE Both Sexes = c(62.9,58.9,65.7 , 53.7,65.8,82.4,81.8,81.2,80.6,77.6,81.1,82.7,81.4,74.6, 73.6 ,81.6,82.8,82.3,70.6,76.1,83.7,68.8,76.7,79.6,73.5, 71.9,74.6,77.8,74.8)),.names = c(“”,“Country”,“ISO”,“Region “, ”Subregion“,”Murder Rate“,”Count“,”GDP“,”GPI“,”percentage_non_religious“, ”Population“,”LE Male“,”LE Female“,”LE Both Sexes“ row.names = c(NA, 29L),class = c(“tbl_df”,“tbl”,“data.frame”))

提前致谢!

+0

请问您可以使用'dput()'给我们提供一个'focusgroup'中的数据样本 – G5W

+0

添加到问题中 – Ekaterina

回答

1

以下是一种使用ggplot2的方法,并通过“fill”参数定义了条形图的颜色。注意我选择了旋转轴的方向。

library(ggplot2) 

### read data 
dat <- data.frame(Country = c("South Africa", "Swaziland", "Botswana", "Lesotho", "Namibia", "Sweden", "Norway", "United Kingdom", "Denmark", "Estonia", "Finland", "Iceland", "Ireland", "Latvia", "Lithuania", "New Zealand", "Australia", "South Korea", "North Korea", "China", "Japan", "Mongolia", "Mexico", "Costa Rica", "El Salvador", "Guatemala", "Honduras", "Panama", "Nicaragua"), 
        GDP = c(10700, 4500, 14000, 1700, 6900, 39100, 54600, 34800, 36600, 19100, 35400, 38300, 37300, 14700, 16000, 27700, 41000, 30000, 1800, 7600, 34000, 3600, 13900, 11300, 7200, 5200, 4200, 13000, 3000), 
        Population = c(54490, 1287, 2262, 2135, 2459, 9779, 5211, 64716, 5669, 1313, 5503, 329, 4688, 1971, 2878, 4529, 23969, 50293, 25155, 1383925, 126573, 2959, 127017, 4808, 6127, 16343, 8075, 3929, 6082), 
        LE_Male = c(59.3, 56.6, 63.3, 51.7, 63.1, 80.7, 79.8, 79.4, 78.6, 72.7, 78.3, 81.2, 79.4, 69.6, 68.1, 80, 80.9, 78.8, 67, 74.6, 80.5, 64.7, 73.9, 77.1, 68.8, 68.5, 72.3, 74.7, 71.5), 
        LE_Both_Sexes = c(62.9, 58.9, 65.7, 53.7, 65.8, 82.4, 81.8, 81.2, 80.6, 77.6, 81.1, 82.7, 81.4, 74.6, 73.6, 81.6, 82.8, 82.3, 70.6, 76.1, 83.7, 68.8, 76.7, 79.6, 73.5, 71.9, 74.6, 77.8, 74.8)) 

我们既可以使用在预期寿命变量的连续值来定义栏的填充颜色...

ggplot(dat, aes(x=reorder(Country, GDP), y=GDP, fill=LE_Both_Sexes)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    xlab("Country") 

......或者说,我们首先必须创建其他使用类在我们的data.frame。要生成一个表示我们类的因子水平的向量,我们可以使用cut

dat$LE_class <- cut(dat$LE_Both_Sexes, breaks=c(50,70,80,90), labels=c("50-70", "70-80", "80-90")) 

ggplot(dat, aes(x=reorder(Country, GDP), y=GDP, fill=LE_class)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    xlab("Country")+ 
    scale_fill_manual(values = c("yellow", "red", "green")) # here's where you define the colors of the classes 
#(imho I would suggest reordering them, as c("red", "yellow", "green") seems more intuitive