## Placental lesion plots library(ggplot2) library(dplyr) library(tidyr) library(ggsci) getwd() setwd("C:/Users/blakebe/Documents/CD1 PFOA GenX/CEBS/Unsorted files/CSV files") p1<-read.csv("Placental lesion incidence by timepoint and group BEB_05152019.csv") #p2<-read.csv("Placental lesion incidence by LITTER timepoint and group BEB_05152019.csv") p17<-subset(p1, timepoint=="E17.5") p17.select<-p17%>%select(group,wnl.p,labyrinth.atrophy.p, labyrinth.congest.p, labyrinth.necrosis.p, nodule.p, clot.p,other.p) p17.percent<-p17.select%>%gather(status, "percent", 2:8)%>% mutate_if(is.numeric, round) p17.percent$group<-factor(p17.percent$group, levels=c("Vehicle control","PFOA 1 mg/kg","PFOA 5 mg/kg", "GenX 2 mg/kg","GenX 10 mg/kg")) write.csv(p17.percent, "E17.5 placental lesions percentage by lesion type.csv", row.names = F) data <- p17.percent%>%dplyr::group_by(group) %>% dplyr::mutate(pos = cumsum(percent) - (0.5 * percent)) data.ordered<-data[order(data$status),] p <- ggplot(data.ordered, aes(x = group, y = percent)) + geom_bar(aes(fill = status), stat="identity") + geom_text(aes(label = percent, y = pos), size = 3) p p4 <- ggplot() + theme_bw()+ theme(axis.text.x = element_text(angle = -45, hjust = 1))+ geom_bar(aes(y = percent, x = group, fill = status), data = data.ordered, stat="identity") + geom_text(data=data.ordered, aes(x=group, y=pos, label=paste0(percent,"%")), size = 4)+ labs(x="Group", y="Percentage") + ggtitle("Incidence of Placental Lesions") p4 setwd("C:/Users/Public/Documents/My Documents/CD1 PFOA GenX/R plots") tiff("Placental lesion percentages WNL vs abnormal.tiff", units="in", width=7, height=5, res=300) dev.off() p1.select2<-p17%>%select(group,wnl.p,labyrinth.atrophy.p, labyrinth.congest.p, labyrinth.necrosis.p, nodule.p, clot.p,other.p) p1.per2<-p1.select2%>%gather(status, "percent", 3:9) p1.round2<-p1.per2 %>% mutate_if(is.numeric, round) #762a83 #af8dc3 #e7d4e8 #f7f7f7 #d9f0d3 #7fbf7b #1b7837 fill2 <- c("#762a83","#af8dc3","#e7d4e8","#f7f7f7","#d9f0d3","#7fbf7b","#1b7837") fill3 <- c("#762a83","#af8dc3","#e7d4e8","#f7f7f7","#d9f0d3","#a6dba0","#5aae61") #fill.new<-c("gray","azure4","darkgoldenrod1","darkgoldenrod","deepskyblue2","steelblue3","deepskyblue4") fill.new<-rev(c('#7fcdbb','#c7e9b4','#ffffcc','#41b6c4','#1d91c0','#225ea8','#0c2c84')) p1.per2$group<-factor(p1.per2$group, levels=c("Vehicle control","PFOA 1 mg/kg","PFOA 5 mg/kg", "GenX 2 mg/kg","GenX 10 mg/kg")) setwd("C:/Users/blakebe/Documents/CD1 PFOA GenX/R plots") tiff("Placental lesion percentages WNL vs abnormal WITH SUBTYPES.tiff", units="in", width=7, height=5, res=300) p5 <- ggplot() + theme_bw()+ theme(axis.text.x = element_text(angle = -45, hjust = 0))+ geom_bar(aes(y = percent, x = group, fill = status), data = p1.per2, stat="identity") + #geom_text(data=p1.round2, aes(x=group, y=percent, label=paste0(percent,"%")), #size = 3, position = position_stack(vjust = 0.5))+ labs(x="\n Group", y="Incidence \n") + ggtitle("Incidence of Placental Lesions") + scale_fill_manual(values=fill.new) + facet_grid(.~timepoint) p5 dev.off() ########################################################## p1.per2.17<-subset(p1.round2, timepoint=="E17.5") #values<-subset(p1.per2.17, rounded > 0) p1.per2.17.ordered <- p1.per2.17[order(p1.per2.17$status),] greys<-c('white', 'gray80','gray65','gray50','gray35','gray20','black') #p2.round<-p1.per2.17 %>% mutate_if(is.numeric, round) p4 <- ggplot() + theme_bw()+ theme(axis.text.x = element_text(angle = -45, hjust = 1))+ geom_bar(aes(y = percent, x = group, fill = status), data = p1.per2.17.ordered, stat="identity", color="black",size=1) + geom_text(data=p1.per2.17.ordered, aes(x=group, y=percent, label=paste0(percent,"%")), size = 4, position = position_stack(vjust = 0.5))+ labs(x="Group", y="Percentage") + ggtitle("Incidence of Placental Lesions") #scale_fill_manual(values=fill) + #facet_grid(.~timepoint) p4