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")
데이터 집합 : Dataset link
선거 예측에 표시된대로 내가 같은 색상 표시 체계를 좀하고 싶습니다구배.
채우기를 조정하려면 'scale_color_gradientn'을 사용하고 있습니다. 대신에 'scale_fill_gradientn'을 사용하십시오. 채우기 위해 매핑 된 데이터가 바이너리 인 것처럼 보이므로 실제로는 'scale_fill_discrete' (또는'_manual','_brewer' 등; 그냥'_continuous' 파생물이 아닙니다)가 필요할 수도 있습니다. – alistaire
@alistaire : 업데이트 주셔서 감사합니다. 그러나 나는 변화가 없을 때까지있다. 의 사용 'scale_fill_discrete'는 여기에서 이와 같은 것을 변경하지 않습니다. – CodeHunter
또한 바이너리 색 구성표가 있지만 여기서 자세히 관찰하면 각 색 구성표가 연속되어 있다고 생각합니다. – CodeHunter