一. 实验目的和要求
1. 了解时间趋势可视化知识,学习与掌握柱状图的生成方法;
2. 学习并掌握R+Illustrator可视化绘图模式。
二. 实验过程
1. 根据《鲜活的数据》第4章4.2.1提供的数据,在R中绘制柱状图,并存为PDF文件;
2. 将上一步所得的PDF文件,导入Illustrator,参考教材图4-5样图,对其修改与修饰,并添上自己的署名。
三. 实验代码参考
// 4.2.1 柱形
hotdogs<-read.csv("http://datasets.flowingdata.com/hot-dog-contest-winners.csv", sep=",", header=TRUE)
hotdogs<-read.csv("hot-dog-contest-winners.csv", sep=",", header=TRUE)
hotdogs$Dogs.eaten
barplot(hotdogs$Dogs.eaten)
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year)
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col = "red", border = NA, xlab="Year", ylab="Hot dogs and buns (HDB) eaten")
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col = "#821122", border = NA, xlab="Year", ylab="Hot dogs and buns (HDB) eaten")
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col = "#cccccc", border = NA, xlab="Year", ylab="Hot dogs and buns (HDB) eaten")
fill_colors<-c()
for(i in 1:length(hotdogs$Country)){
if(hotdogs$Country[i] == "United States")
{
fill_colors <- c(fill_colors, "#821122")
}
else
{
fill_colors <- c(fill_colors, "#cccccc")
}
}
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col = fill_colors, border = NA, xlab="Year", ylab="Hot dogs and buns (HDB) eaten")
fill_colors<-c()
for(i in 1:length(hotdogs$New.record)){
if(hotdogs$New.record[i] == 1)
{
fill_colors <- c(fill_colors, "#821122")
}
else
{
fill_colors <- c(fill_colors, "#cccccc")
}
}
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col = fill_colors, border = NA, space=0.3, xlab="Year", ylab="Hot dogs and buns (HDB) eaten")
rgb(0, 1, 0)
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col="red", border=NA, xlab="Year", ylab="Hot dogs and buns (HDB) eaten")
附图4-5中英文版如下所示: