library(ggplot2) DATA <- read.csv(file.choose(), header=TRUE) DEM.P <- DATA$PE*DATA$DEM OTH.P <- DATA$PE*DATA$OTH GOP.P <- DATA$PE*DATA$GOP TARGET.P <- rep(DATA$GROUP,3) TARGET.P <- factor(TARGET.P, levels=rev(DATA$GROUP)) PARTY.P <- c("Democrats","Others","Republicans") VALUES.P <- c(DEM.P,OTH.P,GOP.P) CILO.P <- rep_len(DATA$CILO,12) CIHI.P <- rep_len(DATA$CIHI,12) FILL.P <- c(rep_len("blue3",12),rep_len("gray",12),rep_len("red3",12)) SUM.P <- DEM.P + OTH.P + GOP.P DATA.F <- data.frame(TARGET.P,PARTY.P,VALUES.P,CILO.P,CIHI.P,FILL.P,SUM.P) ggplot(DATA.F, aes(x=100*VALUES.P, y=TARGET.P, fill=PARTY.P)) + geom_bar(stat="identity", position="stack", color="black", fill=FILL.P, size=0.85, width=0.7) + geom_errorbarh(aes(xmin=100*CILO.P, xmax=100*CIHI.P), height=0, size=0.7) + scale_x_continuous(limits=c(0,100)) + geom_text(aes(x=100, label=scales::percent(SUM.P, accuracy=1L)), position=position_dodge(width=0.7), hjust=1, size=4.5) + labs(title="Percentage that rated\nthe target group under 50", caption="Target groups listed on the left. Ratings were on a 0-to-100 scale in which\n100 is warmest. Percentages include non-responses. Weights for the web\nsample. Error bars are 83.4% confidence intervals. Data: American National\nElection Studies. 2021. ANES 2020 Time Series Study Full Release\n[dataset and documentation]. July 19, 2021 version. www.electionstudies.org.") + 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.5), 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_blank(), 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),"cm"), plot.title = element_text(face="bold", margin=margin(t=0, b=13), size=18, hjust=0.5), plot.subtitle = element_text(hjust=0.5, size=15), plot.caption = element_text(hjust=0, size=9)) ggsave(file="D:R plot ANES 2020 Cold 12 STACKED.svg", width=7, height=5.5)