2017-10-10 62 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参数。