我试图制作一个显示不同国家人均GDP的条形图,以便根据这些国家的预期寿命,其上的条形图有不同的颜色。就目前而言,我可以创建这样一个条形图但只是具有离散的预期寿命值,而不是一段时间。
如果国家,例如,预期寿命从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,30000,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)),. Name = c(“”,“Country”,“ISO”,“Region”,
“分区域”,“谋杀率”,“统计”,“GDP”,“GPI”,“percentage_non_religious”,
“人口”,“LE男”,“LE女”,“LE两性”),row.names = c(NA,
29L),class = c(“tbl_df”,“tbl”,“data.frame”))
提前谢谢!
答案 0 :(得分:1)
这是一种使用ggplot2
并通过&#34; fill&#34;定义条形图颜色的方法。论点。请注意,我选择旋转轴方向。
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