library(foreign)

x<-read.dta("heatmapstates2.dta")
x<-x[complete.cases(x), ]

#x<-x[1:51,]

x$region <- tolower(x$state_name)

library(ggplot2)
library(maps)
states <- map_data("state")
map.df <- merge(states,x, by="region", all.x=T)
map.df <- map.df[order(map.df$order),]

no_axes <- theme(
    plot.background = element_blank(),
    axis.text = element_blank(),
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.title = element_blank()
)

m1<- ggplot(map.df, aes(x=long,y=lat,group=state_name))+
    geom_polygon(aes(fill=AVGratio1), color = "black")+
    xlim(-128, -60)+ylim(25, 50)+
    geom_text(data=x, aes(x=Longitude,y=Latitude, group=NA, label=""),
              size=2.5, vjust=0.5, hjust=0.5)


# add text
library(doBy)
txtVal <- summaryBy(long+lat+AVGratio1 ~ state_name, data=map.df, FUN=c(mean, max, min), keep.names=T)
txtVal$longpos <- (txtVal$long.max + txtVal$long.min)/2
txtVal$latpos <- (txtVal$lat.max + txtVal$lat.min)/2

# subset the pop-out states
txtVal$popout<-0
txtVal[(txtVal$state_name=="NEW HAMPSHIRE")|(txtVal$state_name=="VERMONT")|
                  (txtVal$state_name=="MASSACHUSETTS")|
                  (txtVal$state_name=="RHODE ISLAND")|(txtVal$state_name=="CONNECTICUT")|
                  (txtVal$state_name=="NEW JERSEY")|
                  (txtVal$state_name=="DELAWARE")|(txtVal$state_name=="MARYLAND")|
                  (txtVal$state_name=="DISTRICT OF COLUMBIA"),]$popout<-1



# with popouts
#devtools::install_github("slowkow/ggrepel")
library(ggrepel)
m2 <- m1+ geom_text(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                    data=subset(txtVal, popout=0), col="black", cex=3) +
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                           data=subset(txtVal, state_name=="NEW HAMPSHIRE"), col="black",
                  nudge_x = 8, nudge_y = -1) +
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="VERMONT"), col="black",
                  nudge_x = 9, nudge_y = -2) +
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="MASSACHUSETTS"), col="black",
                  nudge_x = 9, nudge_y = -1.5)+
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="RHODE ISLAND"), col="black",
                  nudge_x = 8, nudge_y = -2.1)+
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="CONNECTICUT"), col="black",
                  nudge_x = 9, nudge_y = -3)+
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="NEW JERSEY"), col="black",
                  nudge_x = 12, nudge_y = -2.5)+
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="DELAWARE"), col="black",
                  nudge_x = 11.5, nudge_y = -2.5)+
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="MARYLAND"), col="black",
                  nudge_x = 13.2, nudge_y = -3.1)+
  geom_text_repel(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
                  data=subset(txtVal, state_name=="DISTRICT OF COLUMBIA"), col="black",
                  nudge_x = 13.1, nudge_y = -4)
m2
## Warning: Removed 1 rows containing missing values (geom_text).

## with boxes instead
## this will look better with a for-loop but I'm lazy today

#a<- rep(62, 9)
#b<-seq(40,28, length.out = 9)
#c<-c("NEW HAMPSHIRE", "VERMONT", "MASSACHUSETTS", "RHODE ISLAND","CONNECTICUT",
#     "NEW JERSEY", "DELAWARE", "MARYLAND", "DISTRICT OF COLUMBIA")

#for(i in 1:9){
#  nam <- paste("A", i, sep = "")
#  assign(nam, geom_rect(aes(xmin = -a[i] - 2, xmax = -a[i] + 2,
#                            ymin = b[i] - 0.8, ymax = b[i] + 0.8,
#                            fill=txtVal[(txtVal$state_name==c[i]),
#                                        ]$AVGratio1.mean),
#                        color = "black"))
#  nam1 <- paste("B", i, sep = "")
#  assign(nam1, geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = b[i]),
#               data = subset(txtVal, state_name==c[i]), colour = "black"))
#
#}


#m1+A1+A2+A3+A4+A5+A6+A7+A8+A9+B1+B2+B3+B4+B5+B6+B7+B8+B9+
#  scale_fill_gradient2(breaks = c(0.5, 1, 2),
#                       trans = "log10") +
#  theme_bw()+ no_axes



m1 + geom_text(aes(x=longpos, y=latpos, label=round(AVGratio1.mean, 2)),
               data=subset(txtVal, popout=0), col="black", cex=3)+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 40 - 0.8, ymax = 40 + 0.8,
                  fill=txtVal[(txtVal$state_name=="NEW HAMPSHIRE"),
                              ]$AVGratio1.mean),
          color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 38.5 - 0.8, ymax = 38.5 + 0.8,
                fill = txtVal[(txtVal$state_name=="VERMONT"),
                              ]$AVGratio1.mean),
             color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 37 - 0.8, ymax = 37 + 0.8,
                fill = txtVal[(txtVal$state_name=="MASSACHUSETTS"),
                              ]$AVGratio1.mean),
            color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 35.5 - 0.8, ymax = 35.5 + 0.8,
                fill = txtVal[(txtVal$state_name=="RHODE ISLAND"),
                              ]$AVGratio1.mean),
            color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 34 - 0.8, ymax = 34 + 0.8,
                fill = txtVal[(txtVal$state_name=="CONNECTICUT"),
                              ]$AVGratio1.mean),
            color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 32.5 - 0.8, ymax = 32.5 + 0.8,
                fill = txtVal[(txtVal$state_name=="NEW JERSEY"),
                              ]$AVGratio1.mean),
            color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 31 - 0.8, ymax = 31 + 0.8,
                fill = txtVal[(txtVal$state_name=="DELAWARE"),
                              ]$AVGratio1.mean),
           color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 29.5 - 0.8, ymax = 29.5 + 0.8,
                fill = txtVal[(txtVal$state_name=="MARYLAND"),
                              ]$AVGratio1.mean),
            color = "black")+
  geom_rect(aes(xmin = -62 - 2, xmax = -62 + 2, ymin = 28 - 0.8, ymax = 28 + 0.8,
                fill = txtVal[(txtVal$state_name=="DISTRICT OF COLUMBIA"),
                              ]$AVGratio1.mean),
            color = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 40),
               data = subset(txtVal, state_name=="NEW HAMPSHIRE"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 38.5),
               data = subset(txtVal, state_name=="VERMONT"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 37),
               data = subset(txtVal, state_name=="MASSACHUSETTS"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 35.5),
               data = subset(txtVal, state_name=="RHODE ISLAND"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 34),
               data = subset(txtVal, state_name=="CONNECTICUT"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 32.5),
               data = subset(txtVal, state_name=="NEW JERSEY"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 31),
               data = subset(txtVal, state_name=="DELAWARE"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 29.5),
               data = subset(txtVal, state_name=="MARYLAND"), colour = "black")+
  geom_segment(aes(x = longpos, y = latpos, xend = -64, yend = 28),
               data = subset(txtVal, state_name=="DISTRICT OF COLUMBIA"), colour = "black")+
  geom_text(data = subset(txtVal, state_name=="NEW HAMPSHIRE"),
            aes(x=-62, y=40, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="VERMONT"),
            aes(x=-62, y=38.5, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="MASSACHUSETTS"),
            aes(x=-62, y=37, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="RHODE ISLAND"),
            aes(x=-62, y=35.5, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="CONNECTICUT"),
            aes(x=-62, y=34, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="NEW JERSEY"),
            aes(x=-62, y=32.5, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="DELAWARE"),
            aes(x=-62, y=31, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="MARYLAND"),
            aes(x=-62, y=29.5, label=round(AVGratio1.mean, 2)), size=2.5)+
  geom_text(data = subset(txtVal, state_name=="DISTRICT OF COLUMBIA"),
            aes(x=-62, y=28, label=round(AVGratio1.mean, 2)), size=2.5) +
  scale_fill_gradient2(breaks = c(0.5, 1, 2),
                       trans = "log10") +
  theme_bw()+ no_axes
## Warning: Removed 1 rows containing missing values (geom_text).