Maria@70: library(rworldmap) Maria@70: library(ggplot2) Maria@70: m@91: library(extrafont) m@91: font_import() m@91: loadfonts() m@91: Arial <- Type1Font(family="Arial", metrics=c("ArialMT.afm","arial-BoldMT.afm","Arial-ItalicMT.afm", "Arial-BoldItalicMT.afm")) m@91: postscriptFonts(Arial=Arial) m@91: par(family="Arial") m@91: Maria@70: PlotBarChart<- function(df, cat="Language", ordercat="REGION", mincount=10, legend=T, color_plt="Paired"){ Maria@70: idx_cat = which(colnames(df)==cat) Maria@70: idx_ordercat = which(colnames(df)==ordercat) Maria@70: dfsub <- subset(df, df[,idx_cat]!="") Maria@70: dfsub <- dfsub[ dfsub[,idx_cat] %in% names(table(dfsub[,idx_cat]))[table(dfsub[,idx_cat]) >mincount] , ] Maria@70: #dfsub <- dfsub[order(dfsub$REGION.y),] Maria@70: dfsub <- dfsub[order(dfsub[,idx_ordercat]),] Maria@70: dfsub[,idx_cat] <- factor(dfsub[,idx_cat], levels=unique(dfsub[,idx_cat])) Maria@70: g = ggplot(dfsub,aes(dfsub[,idx_cat], fill=dfsub[,idx_ordercat], order=-as.numeric(dfsub[,idx_ordercat])))+geom_bar() Maria@70: #g = g+ylim("0", "100")#+scale_y_discrete(breaks=c("100"),labels=c("100+")) Maria@70: g=g+scale_y_continuous(limits=c(0, 200), breaks=seq(0,200,40)) Maria@70: g=g+scale_fill_brewer(palette=color_plt) m@91: g=g+theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5), text = element_text(size=12)) Maria@70: g=g+labs(y="Counts",x=cat)+coord_flip()+theme_bw() Maria@70: if (legend){ Maria@70: g=g+guides(fill = guide_legend(title = ordercat))} Maria@70: else{ Maria@70: g=g+guides(fill="none") Maria@70: } Maria@70: return(g) Maria@70: } Maria@70: Maria@70: PlotCountryCounts <- function(df, output=F){ Maria@70: countrycounts = table(df$Country) Maria@70: dd=data.frame(countrycounts) Maria@70: names(dd)=c("Country","Counts") Maria@70: spdf<-joinCountryData2Map(dd,joinCode="NAME",nameCountryColumn="Country",nameJoinColumn="Country") Maria@70: spdf<-spdf[-which(spdf$ADMIN=='Antarctica'),] Maria@70: #mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=as.numeric(levels(as.factor(spdf$Counts))),missingCountryCol='grey',borderCol='black',oceanCol="white",colourPalette='heat', mapTitle="",addLegend=F) Maria@70: mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=seq(10,100,10),missingCountryCol='grey',borderCol='black',oceanCol="white",colourPalette="heat", mapTitle="",addLegend=F) Maria@70: #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendShrink=0.5,legendWidth=0.5, tcl=0.3, legendMar = 7, legendLabels="all",horizontal=T, legendIntervals="page")) Maria@70: legend("left", legend = c(paste(seq(90,1,-10),'-',seq(100,11,-10)), 'NA'), fill = c(heat.colors(9, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white") Maria@70: if (output){ Maria@70: pdf(file="countrycounts.pdf") Maria@70: mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=seq(10,100,10),missingCountryCol='grey',borderCol='black',oceanCol="white",colourPalette="heat", mapTitle="",addLegend=F) Maria@70: #mapParams <- mapCountryData(spdf, nameColumnToPlot="Counts",catMethod=as.numeric(levels(as.factor(spdf$Counts))),missingCountryCol='grey',borderCol='black',oceanCol="lightblue",colourPalette='heat', mapTitle="",addLegend=F) Maria@70: #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendShrink=0.5,legendWidth=0.5, tcl=0.3, legendMar = 7, legendLabels="all",horizontal=T, legendIntervals="page")) Maria@70: legend("left", legend = c(paste(seq(90,1,-10),'-',seq(100,11,-10)), 'NA'), fill = c(heat.colors(9, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white") Maria@70: dev.off() Maria@70: } Maria@70: } Maria@70: Maria@70: PlotYearDistribution <- function(df, output=F){ Maria@70: df$Year<-as.numeric(as.character(df$Year)) Maria@70: g = ggplot(df,aes(x=Year,y=..count..))+geom_histogram(breaks=seq(1895, 2015, by = 1)) Maria@70: #g = ggplot(df,aes(x=Year,y=..count..))+geom_bar()+geom_density(alpha=.3, fill="grey") Maria@70: #g = g+scale_x_continuous(breaks = pretty(df$Year, n=10)) Maria@70: g = g+theme_bw()+labs(x ='Year', y ='Count') Maria@70: #g = ggplot(df,aes(x=Year,y=..count..))+geom_histogram()+theme_bw() Maria@70: print(g) Maria@70: if (output){ Maria@70: ggsave('yeardistribution.pdf',plot=g) Maria@70: } Maria@70: } Maria@70: Maria@70: PlotLanguageDistribution <- function(df, mincount=1){ Maria@70: dfsubset <- subset(df, df$Language!="") # ignore the recordings culture info Maria@70: culturecounts = table(dfsubset$Culture) Maria@70: culturecounts = culturecounts[culturecounts>=mincount] Maria@70: barplot(culturecounts, las=2, cex.names=0.2) Maria@70: #g = ggplot(df,aes(x=Language))+geom_bar() Maria@70: #g+theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) Maria@70: } Maria@70: Maria@70: PlotCountryOutliers <- function(df, output=''){ Maria@70: par(mar = rep(2, 4)) Maria@70: spdf<-joinCountryData2Map(df,joinCode="NAME",nameCountryColumn="Country",nameJoinColumn="Country") Maria@70: spdf<-spdf[-which(spdf$ADMIN=='Antarctica'),] Maria@70: #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers",catMethod=seq(0,70,5),missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE) Maria@70: mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE) Maria@70: # avoid antarctica Maria@70: #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", ylim=c(-60,90), catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE) Maria@70: #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendWidth=0.5, tcl=0.3, legendMar = 7, legendLabels="all",horizontal=T, legendIntervals="page")) Maria@70: legend("left", legend = c(paste(seq(90,0,-10),'-',seq(100,10,-10),'%'), 'NA'), fill = c(heat.colors(10, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white") Maria@70: if (output!=''){ m@91: if (grepl('.pdf', output)){ m@91: pdf(output, pointsize=12) m@91: } else if (grepl('.eps', output)){ m@91: postscript(output, pointsize=12) m@91: } Maria@70: #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers",catMethod=seq(0,70,5),missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE) Maria@70: mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE) Maria@70: #mapParams <- mapCountryData(spdf, nameColumnToPlot="Outliers", ylim=c(-60,90), catMethod=seq(0,1,0.1), missingCountryCol='grey',colourPalette='heat', mapTitle="", addLegend=FALSE) Maria@70: #do.call( addMapLegend, c(mapParams, labelFontSize=0.7, legendWidth=0.5, tcl=0.3, legendMar=7, legendLabels="all",horizontal=T, legendIntervals="page")) Maria@70: legend("left", legend = c(paste(seq(90,0,-10),'-',seq(100,10,-10),'%'), 'NA'), fill = c(heat.colors(10, alpha = 1), 'grey'), cex = 0.56, bty = "o",bg="white",box.lwd=0,box.col="white") Maria@70: dev.off() Maria@70: } Maria@70: else { Maria@70: return(mapParams) Maria@70: } Maria@70: }