#Chris Cochrane, June 27, 2012 library(ggplot2) library(reshape2) library(plyr) library(scales) library(grid) library(RColorBrewer) memory.limit(10000) #loading Census Data 2001 AllImmigs <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/immigsonly.csv") Muslims <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/Muslims.csv") Buddhist <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/Buddhist.csv") Catholics <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/Catholics.csv") HinduSikh <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/HinduSikh.csv") Jews <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/Jews.csv") None <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/None.csv") Other <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/Other.csv") Protestants <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Census/Protestants.csv") #Variable for immigrant status Full$ImmigrantText[Full$Immigrant==1] <- "Immigrant" Full$ImmigrantText[Full$Immigrant==0] <- "Native" Full$ReligionText <- as.character(Full$Religion) Full$Position[Full$ReligionText=="1.Muslim"] <- .53 Full$Position[Full$ReligionText=="2.Buddhist"] <- .51 Full$Position[Full$ReligionText=="3.Hindu/Sikh"] <- .40 Full$Position[Full$ReligionText=="4.Jewish"] <- -.2 Full$Position[Full$ReligionText=="5.Other"] <- -.22 Full$Position[Full$ReligionText=="6.None"] <- -.38 Full$Position[Full$ReligionText=="7.Protestant"] <- -.47 Full$Position[Full$ReligionText=="8.Catholic"] <- -.56 #R seems incapable of rendering the graph that I want with the large data set Religion <- c("1.Muslim", "2.Buddhist", "3.Hindu/Sikh", "4.Jewish", "5.Other", "6.None", "7.Protestant", "8.Catholic") Religion <- rep(Religion, each=2) Religion2 <- c("Muslim", "Buddhist", "Hindu/Sikh", "Jewish", "Other", "None", "Protestant", "Catholic") Religion2 <- rep(Religion2, each=2) ImmigrantText <- c("Native", "Immigrant") ImmigrantText <- rep(Immigrant, 8) Immigrant <- c(0,1) Immigrant <- rep(Immigrant,8) ImmigvNative <- c(.234, .766, .246, .754, .298, .702, .679, .320, .714, .286, .790, .210, .817, .183, .847, .152) Position <- c(.43, .38, .38, -.28, -.28, -.28, -.38, -.38) Position <- rep(Position, each=2) Position <- as.character(Position) Position <- as.numeric(Position) Full.Gimped <- data.frame(cbind(Religion, Religion2, Immigrant, ImmigrantText, ImmigvNative, Position)) Full.Gimped$Position <- as.character(Full.Gimped$Position) Full.Gimped$Position <- as.numeric(Full.Gimped$Position) Full.Gimped$ImmigvNative <- as.character(Full.Gimped$ImmigvNative) Full.Gimped$ImmigvNative <- as.numeric(Full.Gimped$ImmigvNative) Full.Gimped$Religion2 <- as.character(Full.Gimped$Religion2) rm(Religion, Religion2, ImmigrantText, Immigrant, ImmigvNative, Position) Immig.Graph <- ggplot(Full.Gimped, aes(Religion)) + geom_bar(subset = .(Immigrant==1), aes(y=ImmigvNative, fill=ImmigrantText)) + geom_bar(subset = .(Immigrant==0), aes(y = -ImmigvNative, fill=ImmigrantText)) + geom_text(aes(x=Religion, y=Position, label=as.character(Religion2)), angle=-90, size=3) + scale_y_continuous(breaks=seq(-.8, 8, .2), labels = percent_format()) + ylab(" ") + xlab("Religious Groups") + opts(title="Immigration Status") + opts(plot.title=theme_text(size =15)) + opts(legend.title=theme_blank()) + opts(axis.text.y = theme_text(size=15)) + opts(axis.text.x = theme_blank()) + geom_hline(yintercept=0) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.85,.9)) + opts(axis.title.x = theme_text(size=15)) Muslim.Graph <- ggplot(Muslims, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="Muslim Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.85,.70)) Buddhist.Graph <- ggplot(Buddhist, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="Buddhist Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.85,.7)) + opts(legend.title = theme_blank()) HinduSikh.Graph <- ggplot(HinduSikh, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="Hindu/Sikh Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.85,.75)) + opts(legend.title = theme_blank()) Jews.Graph <- ggplot(Jews, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="Jewish Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.text = theme_text(size=10)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.15,.75)) + opts(legend.title = theme_blank()) None.Graph <- ggplot(None, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="No Religion Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.85,.75)) + opts(legend.title = theme_blank()) Other.Graph <- ggplot(Other, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="Other Religion Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.85,.75)) + opts(legend.title = theme_blank()) Protestant.Graph <- ggplot(Protestants, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="Protestant Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.15,.75)) + opts(legend.title = theme_blank()) Catholic.Graph <- ggplot(Catholics, aes(YearsinCountry)) + geom_bar(aes(y = ..count../sum(..count..), fill=Birthplace)) + scale_x_continuous(limits=c(0, 36), breaks=seq(0,36,5) ) + ylab("") + xlab("Years in Country") + scale_y_continuous(limits=c(0,.12), labels=percent_format()) + opts(title="Catholic Immigrants") + opts(plot.title=theme_text(size =15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(legend.text = theme_text(size=10)) + opts(legend.title = theme_text(size=10)) + opts(legend.key.size = unit(3, "mm")) + opts(legend.position = c(.15,.75)) + opts(legend.title = theme_blank()) #Multiplot function from http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/ multiplot <- function(..., plotlist=NULL, cols) { require(grid) # Make a list from the ... arguments and plotlist plots <- c(list(...), plotlist) numPlots = length(plots) # Make the panel plotCols = cols # Number of columns of plots plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols # Set up the page grid.newpage() pushViewport(viewport(layout = grid.layout(plotRows, plotCols))) vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) # Make each plot, in the correct location for (i in 1:numPlots) { curRow = ceiling(i/plotCols) curCol = (i-1) %% plotCols + 1 print(plots[[i]], vp = vplayout(curRow, curCol )) } } All.Graphs <- multiplot(Immig.Graph, Muslim.Graph, Buddhist.Graph, HinduSikh.Graph, Jews.Graph, Other.Graph, None.Graph, Protestant.Graph, Catholic.Graph, cols=3) CountryData <- read.csv(file = "C:/Dropbox/Projects/Muslims/Data/WVSHDI/CountryLevelData.csv") CountryData$CountryName <- as.character(CountryData$Country) #p <- ggplot(CountryData, aes(x=HDI, y=Homosexuality, size=Muslims_per)) + # geom_smooth(legend=FALSE) + # geom_point(aes(colour=Region), alpha=.9) + # labs(colour= "Region") + # geom_point(aes(size=Muslims_per), shape=1) + # labs(size="Muslims (%)") + # scale_area(range=c(2,20)) + # xlab("Human Development Index") + # ylab("Mean Opinion re: Homosexuality") + # opts(legend.title = theme_text(size=15)) + # opts(legend.text = theme_text(size=10)) + # opts(axis.title.y = theme_text(size=15, angle=90)) + # opts(axis.title.x = theme_text(size=15)) p <- ggplot(CountryData, aes(x=HDI, y=Homosexuality)) + geom_smooth(legend=FALSE) + geom_point(aes(shape=Region, fill=Region, size=Muslims_per), alpha=.8, legend=TRUE) + scale_shape_manual(values=c(22, 25, 21, 24, 12, 23)) + labs(colour= "Region") + labs(size="Muslims") + scale_area(range=c(2,20), label=percent) + xlab("\nHuman Development Index") + ylab("Mean Opinion about Homosexuality\n") + opts(legend.title = theme_text(size=15)) + opts(legend.text = theme_text(size=15)) + opts(axis.title.y = theme_text(size=15, angle=90)) + opts(axis.title.x = theme_text(size=15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + scale_x_continuous(breaks=c(.3, .4, .5, .6, .7, .8, .9)) #Coding in Stata, and then import the merged file Ipsos <- read.csv(file="C:/Dropbox/Projects/Muslims/Data/Ipsos/Merged0611.csv") rm(Religions, ReligionsChar, GayMarriage, Support) Religions <- c("4.Catholic (n=24e3)", "2.Protestant (n=27e3)", "1.Muslim (n=363)", "6.Jewish (n=1065)", "3.Hindu/Sikh (n=232)", "5.Other (n=3457)", "7.No Religion (n=14e3)") Religions <- rep(Religions, each=3) ReligionsChar <- c("Catholic", "Protestant", "Muslim", "Jewish", "Hindu/Sikh", "Other", "No Religion") ReligionsChar <- rep(ReligionsChar, each=3) GayMarriage <- c(1,2,3) GayMarriage <- rep(GayMarriage, 7) #Support is proportion supporting + 1/2 indiff, proportion opposing + 1.2 indiff Support <- c(.716, .169, .285, .625, .173, .374, .438, .121, .562, .84, .105, .16, .701, .106, .3, .785, .09, .215, .873, .08, .128) Ipsos.Gimped <- data.frame(cbind(Religions, ReligionsChar, GayMarriage, Support)) Ipsos.Gimped$Support <- as.character(Ipsos.Gimped$Support) Ipsos.Gimped$Support <- as.numeric(Ipsos.Gimped$Support) Ipsos.Gimped$ReligionsChar <- as.character(Ipsos.Gimped$ReligionsChar) Ipsos.Gimped$GayMarriage <- as.character(Ipsos.Gimped$GayMarriage) Ipsos.Gimped$GayMarriage <- as.numeric(Ipsos.Gimped$GayMarriage) attach(Ipsos.Gimped) Ipsos.Gimped$GayMarriageChar[GayMarriage==1] <- "1.Marriage" Ipsos.Gimped$GayMarriageChar[GayMarriage==2] <- "2.Civil Unions" Ipsos.Gimped$GayMarriageChar[GayMarriage==3] <- "3.No Recognition" detach(Ipsos.Gimped) GayMarriage.Graph <- ggplot(Ipsos.Gimped, aes(Religions)) + geom_bar(subset = .(GayMarriage==1), aes(y=Support, fill=GayMarriageChar), color="black") + geom_bar(subset = .(GayMarriage==3), aes(y = -Support, fill=GayMarriageChar), color="black") + geom_bar(subset = .(GayMarriage==2), aes(y = Support, fill=GayMarriageChar), color="black") + geom_bar(subset = .(GayMarriage==2), aes(y = -Support, fill=GayMarriageChar), color="black") + scale_y_continuous(breaks=seq(-.9, .9, .2), labels = percent_format()) + ylab(" ") + xlab(" ") + opts(legend.title=theme_blank()) + opts(legend.text=theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.text.x = theme_text(size=15)) + geom_hline(yintercept=0, legend=FALSE) + opts(axis.title.x = theme_text(size=15)) + scale_fill_brewer(palette= "YlOrRd") + coord_cartesian(ylim= c(-.9, .9)) + opts(legend.position="bottom") + coord_flip() GayMarriage.Graph GayMarriage.Graph <- ggplot(Ipsos.Gimped, aes(Religions)) + geom_bar(subset = .(GayMarriage==1), aes(y=-Support, fill=GayMarriageChar), color="black") + geom_bar(subset = .(GayMarriage==3), aes(y = Support, fill=GayMarriageChar), color="black") + geom_bar(subset = .(GayMarriage==2), aes(y = -Support, fill=GayMarriageChar), color="black") + geom_bar(subset = .(GayMarriage==2), aes(y = Support, fill=GayMarriageChar), color="black") + scale_y_continuous(breaks=seq(-.9, .9, .2), labels = percent_format()) + ylab(" ") + xlab(" ") + opts(legend.title=theme_blank()) + opts(legend.text=theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.text.x = theme_text(size=15)) + geom_hline(yintercept=0, legend=FALSE) + opts(axis.title.x = theme_text(size=15)) + scale_fill_brewer(palette= "YlOrRd") + coord_cartesian(ylim= c(-.9, .9)) + opts(legend.position="bottom") + coord_flip() GayMarriage.Graph #Generating data for Opposition to Gay Marriage by Church Attendance, with size #variable for proportion of each religious group within each category of church #attendance #Removing all objects from workspace rm(list=ls()) #Religion Variable, each of six religion labels repeating four times Religion <- c("2.Catholic", "5.Protestant", "6.Muslim", "1.Jewish", "4.Hindu/Sikh", "3.Other") Religion <- rep(Religion, each=4) #Level of Church Attendance, each of four levels repeating for 6 religions ChurchAtt.Level <- c("1.Less Often", "2.Monthly", "3.Weekly", "4.More Often") ChurchAtt.Level <- rep(ChurchAtt.Level, 6) #Proportion of each religion within each category of Church Att Density <- c(.7645, .0859, .1246, .0250, .6639, .0938, .1673, .0750, .5455, .1253, .1376, .1919, .8014, .0957, .0713, .0316, .6450, .1641, .1374, .0534, .8117, .0763, .0694, .0426) #Proportion of each level of Churchatt within each Religion opposing all recognition of gay marriage GayMarr.Oppose <- c(.0884, .1383, .2177, .3618, .1122, .1838, .3972, .5551, .3420, .50, .5577, .5775, .0377, .0577, .1316, .3030, .1533, .1538, .2759, .5385, .0777, .1907, .3908, .4490) #A gimped data set for the graph Ipsos.Gimped <- data.frame(cbind(Religion, ChurchAtt.Level, Density, GayMarr.Oppose)) Ipsos.Gimped$Religion <- as.character(Ipsos.Gimped$Religion) Ipsos.Gimped$Density <- as.character(Ipsos.Gimped$Density) Ipsos.Gimped$Density <- as.numeric(Ipsos.Gimped$Density) Ipsos.Gimped$GayMarr.Oppose <- as.character(Ipsos.Gimped$GayMarr.Oppose) Ipsos.Gimped$GayMarr.Oppose <- as.numeric(Ipsos.Gimped$GayMarr.Oppose) label(Ipsos.Gimped$ChruchAtt.Prop) <- "Density" p <- ggplot(Ipsos.Gimped, aes(x=ChurchAtt.Level, y=GayMarr.Oppose, size=Density), facets=~.Religion) + scale_y_continuous(labels=percent) + xlab("\nFrequency of Religious Attendance") + ylab("% Opposed to All Forms of Legal Recognition\n") + geom_point(aes(colour=Religion), show_guide=FALSE) + geom_point(shape=1, name="Density") + opts(legend.title=theme_blank()) + opts(legend.text=theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.title.x = theme_text(size=15)) + opts(axis.title.y = theme_text(size=15, angle=90)) + scale_area(range = c(2,10), labels=percent) + facet_grid(. ~ Religion) + opts(axis.text.x = theme_text(size=15, angle=90)) + opts(strip.text.x = theme_text(size=15)) + geom_smooth(aes(group=Religion, colour=Religion), show_guide=FALSE) #Generating data for Opposition to Gay Marriage by Education, with size #variable for proportion of each religious group within each category of education #Removing all objects from workspace rm(list=ls()) #Religion Variable, each of six religion labels repeating forteen times Religion <- c("2.Catholic", "5.Protestant", "6.Muslim", "1.Jewish", "4.Hindu/Sikh", "3.Other") Religion <- rep(Religion, each=14) #Level of Education, each of seven levels repeating twice for 6 religions to make room for data about native born and foreign born Educ.Level <- c("1. Univ")) + xlab("\nLevel of Education") + ylab("Estimates\nOppose All Forms Support Marriage") + geom_hline(yintercept=0) + opts(axis.title.y = theme_text(size=15, angle=90)) + opts(axis.title.x = theme_text(size=15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + facet_grid(.~Group) + opts(strip.text.x = theme_text(size = 15)) + geom_text(aes(X, Y, label = Text), data.frame(X=1, Y=.65, Text="All Canadians")) + geom_line(aes(y=FBMus.Oppose.Est), color="Green") + geom_line(aes(y=AllCdn.Oppose.Est), color="Red") geom_point(aes(y=LengthofResidence.Estimate, size=MuslimsPer), color="White") + geom_point(aes(y=LengthofResidence.Estimate, size=MuslimsPer), shape=1, color="Blue") + geom_point(aes(y=LengthofResidence.Estimate, size=ImmigsPer), shape=0, color="Red") + scale_size_manual(name="Density") + scale_y_continuous(limits=c(.2,.65), labels=percent_format()) + xlab("Decades of Residence in Canada") + ylab("% Supporting Same-Sex Marriage\n") + opts(axis.title.y = theme_text(size=15, angle=90)) + opts(axis.title.x = theme_text(size=15)) + opts(axis.text.x = theme_text(size=15)) + opts(axis.text.y = theme_text(size=15)) + scale_area(range=c(2,20), name="Density", labels=percent_format()) + opts(legend.title = theme_text(size=15)) + opts(legend.text = theme_text(size=15))