library(ggplot2) library(grid) DATA <- read.csv(file.choose(), header=TRUE) DATA$GROUP <- factor(DATA$GROUP, levels=rev(c("Jews","Hispanics","Asians/Asian-Americans","Blacks","Rural Americans","Whites","Christians","Gay men and lesbians","Muslims","Transgender people","Christian fundamentalists","Illegal immigrants"))) DATA$FACET <- factor(DATA$FACET, levels=c("Democrats","Republicans")) text.DEM <- textGrob("Democrats", gp=gpar(fontsize=15)) text.GOP <- textGrob("Republicans", gp=gpar(fontsize=15)) theme.z <- theme( plot.background = element_rect(fill="white"), strip.background = element_rect(color="black", fill="black"), strip.text.x = element_text(color="white", face="bold", size=18, margin=margin(t=7.5, b=7.5)), panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), panel.background = element_rect(fill="gray90", color="black", size=0.5, linetype="solid"), panel.border = element_rect(fill=NA, color="black", linetype="solid", size=1), panel.spacing.x = unit(1, "lines"), panel.spacing.y = unit(1, "lines"), axis.title.y = element_blank(), axis.title.x = element_blank(), axis.ticks.y = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_text(size=12, color="black", margin=margin(t=7, b=5)), axis.text.y = element_text(size=14, color="black", margin=margin(l=7, r=7)), plot.margin = unit(c(0.5,0.5,0.5,0.5),"lines"), plot.title = element_text(face="bold", margin=margin(t=0, b=35), size=18, hjust=0.5), plot.subtitle = element_text(hjust=0.5, size=15), plot.caption = element_text(hjust=0, size=9), legend.position = "none") ggplot(DATA, aes(x=100*PE, y=GROUP, fill=FACET)) + geom_bar(stat="identity", color="black", size=0.85, width=0.7) + geom_errorbarh(aes(xmin=100*CILO, xmax=100*CIHI), height=0, size=0.7) + scale_x_continuous(limits=c(-100,100), breaks=seq(-100,100,20), labels=as.character(c(seq(100,0,-20), seq(20,100,20)))) + scale_fill_manual(values=c("blue3", "red3"), name="", breaks=c("Democrats","Republicans"), labels=c("Democrats","Republicans")) + geom_text(data=subset(DATA, FACET=="Democrats"), aes(x=-100, label=scales::percent(-PE, accuracy=1L)), position=position_dodge(width=0.7), hjust=0, size=4.5) + geom_text(data=subset(DATA, FACET=="Republicans"), aes(x=100, label=scales::percent(PE, accuracy=1L)), position=position_dodge(width=0.7), hjust=1, size=4.5) + labs(title="Percentage that rated the target group under 50", caption="Target groups listed on the left. Ratings were on a 0-to-100 scale in which 100 is warmest. Percentages include\nnon-responses. Weights for the web sample. Error bars are 83.4% confidence intervals. Data: American National\nElection Studies. 2021. ANES 2020 Time Series Study Full Release [dataset and documentation]. July 19, 2021\nversion. www.electionstudies.org.") + annotation_custom(text.DEM, xmin=-50, xmax=-50, ymin=13.5, ymax=13) + annotation_custom(text.GOP, xmin= 50, xmax= 50, ymin=13.5, ymax=13) + coord_cartesian(clip="off") + theme.z ggsave(file="D:R plot ANES 2020 Cold 12 DEM GOP B2B.svg", width=9, height=7)