ggplot离散传奇连续数据

时间:2017-10-10 02:55:39

标签: r csv dictionary ggplot2 cartogram

我是R编程的新手,但我很享受编写代码的挑战! 我通过将多个地图拼接在一起来创建了一个GIF。不幸, 我的图例引用了生成的地图的特定年份,因此,GIF显示了一个标记上下移动的图例。我认为解决方案是让图例引用整个数据框而不是给定年份。我该怎么做?

链接到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(kansas1950@data[, "FIPS"], smaller.data1950[, "FIPS"])
kansas1950@data <- data.frame(kansas1950@data, 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, kansas1950@data$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, kansas1950@data, 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()

1 个答案:

答案 0 :(得分:0)

假设这是您想要的,请尝试将其添加到您的绘图中(但是,当然,指定您自己的自定义下限和上限):

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

我有一个有效的样本:

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参数。