2017-02-16 11 views
3

私は、異なる州の大統領結果を表示するために、米国の地図で背景色を変更しようとしています。私はこの色の変化に関する多くの記事を読んだが、私はこれらの色を変更することができなかった。以下は、私が取得しています私のコード、データセットおよびスナップショットへのリンクは次のとおりです。アメリカの地図でRの色を変更できません

#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

選挙予測に表示されているように、私は同じカラーリングスキームを取得したいと思い

勾配。

+7

塗りつぶしを調整するには、scale_color_gradientnを使用しています。代わりに 'scale_fill_gradientn'を使用してください。 fillにマップされたデータはバイナリであるようですが、実際には 'scale_fill_discrete'(または '_manual'、' _brewer'など; '_continuous'の派生物ではありません)だけが必要です。 – alistaire

+0

@alistaire:ありがとうございました。しかし、私は変化が見えなくなるまで待っています。 の使用 'scale_fill_discrete'はここでは何も変更しません。 – CodeHunter

+0

また、私たちはバイナリカラースキームを持っていますが、ここで注意深く観察すれば、それぞれのカラースキームも連続していると思います。 – CodeHunter

答えて

0

上記の問題に対する貴重なフィードバックと解決策を提供してくれたAlistaireに感謝します。 scale_fill_brewer(type = 'qual', palette = 6)ggplot()を使用すると、上記の問題がRで解決されます。

関連する問題