无法在美国地图中更改R中的颜色

时间:2017-02-16 21:41:25

标签: r ggplot2

我正在尝试更改美国地图中的背景颜色,以显示不同州的总统选举结果。我读了很多关于这种颜色变化的帖子,但我无法改变任何这些颜色。下面是我的代码,我收到的数据集和快照的链接:

#install.packages("ggplot2")
#install.packages("ggmap")
#install.packages("plyr")
#install.packages("raster")
#install.packages("stringr")

library(ggplot2) # for plotting and miscellaneuous things
library(ggmap) # for plotting
library(plyr) # for merging datasets
library(raster) # to get map shape filegeom_polygon
library(stringr) # for string operation

# Get geographic data for USA
usa.shape<-getData("GADM", country = "usa", level = 1)   

# Creating a data frame of map data
usa.df <- map_data("state")    

#rename 'region' as 'state' and make it a factor variable 
colnames(usa.df) [5] <- "State"
usa.df$State <- as.factor(usa.df$State)

#set working directory
setwd("C:/Users/Ashish/Documents/Stats projects/2/")

#input data from file separated by  commas
usa.dat <- read.csv("data1.csv", header = T)

# printing data structure
str(usa.df)

# removing % sign from the data, and converting percentage win to numeric
usa.dat$Clinton <- as.numeric(sub("%","",usa.dat$Clinton))/1
usa.dat$Trump <- as.numeric(sub("%","",usa.dat$Trump))/1
usa.dat$Others <- as.numeric(sub("%","",usa.dat$Others))/1


# Creating a winner column based on the percentage 
usa.dat$Winner = "Trump"
usa.dat[usa.dat$Clinton > usa.dat$Trump,]$Winner = "Clinton"
usa.dat$State <- tolower(usa.dat$State)

# Creating a chance column which corresponds to winning percentage of the         candidate
usa.dat$chance <- usa.dat$Trump
a <- usa.dat[usa.dat$Clinton > usa.dat$Trump,]
usa.dat[usa.dat$Clinton > usa.dat$Trump,]$chance <- a$Clinton

# display the internal structure of the object
usa.dat

#join the usa.df and usa.dat objects on state variable
usa.df <- join(usa.df, usa.dat, by = "State", type = "inner")

str(usa.df)
states <- data.frame(state.center, state.abb) # centers of states and abbreviations

#function for plotting different regions of USA map based on the input data       showing different coloring scheme 
#for each state.
p <- function(data, title) {
  ggp <- ggplot() +
    #       Draw borders of states
    geom_polygon(data = data, aes(x = long, y = lat, group = group, 
                              fill = Winner, alpha=chance), color = "black", size = 0.15) + 


#scale_alpha_continuous(range=c(0,1))+
scale_color_gradientn(colours = c("#F08080","white","#5DADE2"),breaks = c(0,50,100), 
                      labels=c("Clinton","Equal","Trump"),
                      limits=c(0,100),name="Election Forecast") +

#       Add state abbreviations     
geom_text(data = states, aes(x = x, y = y, label = state.abb), size = 2)+
guides(fill = guide_legend(direction='vertical',  title='Candidate', label=TRUE, colours=c("red", "blue")))
  return(ggp)
}

figure.title <- "2016 presidential election result"

# Save the map to a file to viewing (you can plot on the screen also, but it takes
# much longer that way. The ratio of US height to width is 1:9.)
#print(p(usa.df, brks.to.use, figure.title))

ggsave(p(usa.df, figure.title), height = 4, width = 4*1.9,
       file = "election_result.jpg")

图片链接:USA map as my output

数据集:Dataset link

我想获得与选举预测渐变中显示的相同的着色方案。

1 个答案:

答案 0 :(得分:0)

感谢Alistaire为上述问题提供了宝贵的反馈和解决方案。使用std::movescale_fill_brewer(type = 'qual', palette = 6)可以解决上述问题。