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