2017-10-10 70 views
0

我是R編程新手,但我很享受編寫代碼的挑戰! 我通過將多個地圖拼接在一起創建了一個GIF。不幸的是,我的圖例引用了正在生成的地圖的特定年份,因此,GIF顯示了一個圖標,其標記上下移動。我認爲解決方案是讓圖例引用整個數據框而不是指定的年份。我該怎麼做呢?ggplot分立傳奇連續數據

鏈接到GIF: https://1drv.ms/i/s!Ap-NxMqZOClHqgsFHSxo-kR1pLrr

##This is the R-Code I used for the year 1950: 

kansas1950 <- readShapePoly("KansasCOUNTIES.shp") 

## Kansas Winter-Wheat Planted from Quickstats 

kansas1950.acres <- read.csv(file = "KWW 19502016 QuickStatsEst.csv", 
stringsAsFactors = FALSE) 

## Create a smaller dataset by retaining the kansas Acres in 1950 and the FIPS 
## FIPS, which will be used for matching and merging with the input shapefile 
smaller.data1950 <- data.frame(FIPS = kansas1950.acres$FIPS, Acres = kansas1950.acres$X1950) 
smaller.data1950 <- na.omit(smaller.data1950) 
## Join the two datasets using their common field 
matched.indices1950 <- match([email protected][, "FIPS"], smaller.data1950[, "FIPS"]) 
[email protected] <- data.frame([email protected], smaller.data1950[matched.indices1950, ]) 

## Compute the cartogram transformation of each county using its population 
## with the degree of Gaussian blur = 0.5 
kansas1950.carto <- quick.carto(kansas1950, [email protected]$Acres, blur = 0.5) 
## Convert the object into data frame 
kansas1950.carto <- gBuffer(kansas1950.carto, byid=TRUE, width=0) 
kansas1950.f <- fortify(kansas1950.carto, region = "FIPS") 
## Merge the cartogram transformation with the kansas map shapefile 
kansas1950.f <- merge(kansas1950.f, [email protected], by.x = "id", by.y = "FIPS") 
# Plot of the transformed polygons, where each county is 
## further shaded by their acreage (lighter means bigger) 

my_map1950 <- ggplot(kansas1950.f, aes(long, lat, group = group, 
fill = kansas1950.f$Acres)) + geom_polygon() + 
scale_fill_continuous(breaks = c(0, 10000, 100000, 200000, 526000), 
     labels = c("0 Acres","10k Acres", "100k Acres", "200k Acres", "526k Acres"), 
     low = "black", 
     high = "purple" 
) + 
labs(x=NULL, y=NULL) + labs(fill = "Acres Planted") 
# Remove default ggplot layers 
my_map1950 <-my_map1950 + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), axis.ticks=element_blank(), 
     axis.text.x=element_blank(),axis.text.y=element_blank(), 
     axis.line = element_line(colour = NA)) 
# Citation 
my_map1950 <- my_map1950 + labs(caption = "USDA-NASS Quick Stats") + ggtitle("1950 Kansas Winter-Wheat Acres Planted") 
my_map1950 
# Save a higher resolution PNG 
png('my_map1950kwwpurp.png', units="in", width=10, height=8, res=300) 
my_map1950 
dev.off() 
+0

歡迎來到SO。請首先閱讀[MCVE]上的此指南(https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),以「幫助其他人幫助您」。 – Ashish

回答

0

假設這是你想要的,你可以添加以下到您的陰謀(但,當然,指定自定義的下限和上限):

+ scale_fill_gradient(limits = c(0, 10)) 

我有工作了樣品DF:

df <- data.frame(x = 1:10) 
p <- ggplot(df, aes(x, 1)) + geom_tile(aes(fill = x), colour = "white") 
p + scale_fill_gradient(limits = c(0, 10)) 
p + scale_fill_gradient(limits = c(0, 20)) 

Here's the graph with the scale set from 0 to 10.

Here's the graph with the scale set from 0 to 20.

編輯:哦,我現在看到你已經在你的代碼中調用scale_fill_continuous()。嘗試添加一個類似於我所做的參數的limits參數。