使用ggplot

时间:2017-09-22 12:14:48

标签: r ggplot2

一张图片胜过千言万语:

enter image description here

从图中可以看出,我创建了一个带有ggplot的刻面图,但是每个方面都缺少那些对该特定变量没有任何价值的国家(尽管这并不能解释像var3中那些灰色国家小)。我的猜测是在下面添加一个新的几何图层会解决这个问题,但不幸的是我没有任何线索可以解决这个问题。

期望的输出:

我想要实现的是,所有国家/地区都显示在所有方面,如果它们没有该特定值的值,则应以另一种颜色(例如灰色)输出。通过这样做,世界地图将永远完整,而不是呈现洞,这使得阅读真的很奇怪。

我想实现这样的目标,但是对于每个方面(显然值和颜色都必须改变):

enter image description here

这是我到目前为止使用的代码:

library(rgdal)
library(dplyr)
library(broom)
library(ggplot2)

world = readOGR(dsn = "https://gist.githubusercontent.com/ccamara/fc26d8bb7e777488b446fbaad1e6ea63/raw/a6f69b6c3b4a75b02858e966b9d36c85982cbd32/countries.geojson")


# Load dataframe.
df = read.csv("https://gist.githubusercontent.com/ccamara/fc26d8bb7e777488b446fbaad1e6ea63/raw/519c6199c9fc24df87bb6ca0bfcd0413db6e3617/sample-dataframe-grouped.csv")

world.t = tidy(world, region = "name")

# join with the original file's data
world.t = left_join(world.t, world@data, by = c("id" = "name"))

# join with df
world.t = left_join(world.t, df, by = c("iso_a2" = "country_code"))

ggplot(data = world.t, aes(long, lat, fill = value, group = group)) +
  facet_wrap(~variable) +
  scale_fill_gradient(low = "yellow", high = "red") +
  #scale_fill_brewer(type = "seq", palette = "Reds")+
  geom_polygon() +
  geom_path(colour = "black", lwd = 0.05) +
  coord_equal() +
  ggtitle("Countries without values are not displayed") +
  labs(fill = "amount") +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank())

此外,在阅读this other question之后,我稍微调整了它,但新图层显示在之前的图层上而不是它们之下。

map.1 = ggplot(data = world.t,
       aes(long, lat, fill = value, group = group)) +
  facet_wrap(~variable) +
  scale_fill_gradient(low = "yellow", high = "red") +
  geom_polygon() +
  geom_path(colour = "black", lwd = 0.05) +
  coord_equal() +
  ggtitle("New layer hides everything else") +
  labs(fill = "amount") +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank())


map.1 + geom_polygon(data=world.t,
            aes(x=long, y=lat, group=group), lwd = 0.01, fill = "blue", inherit.aes = F)

enter image description here

1 个答案:

答案 0 :(得分:3)

我修改了你的代码,以便在开头捕获世界地图:

base_world = tidy(world, region = "name") 

world.t = base_world

# join with the original file's data
world.t = left_join(world.t, world@data, by = c("id" = "name"))

# join with df
world.t = left_join(world.t, df, by = c("iso_a2" = "country_code"))

world.t <- filter(world.t, !is.na(variable))

ggplot(data = world.t[!is.na(world.t$value),],
               aes(long, lat, fill = value, group = group)) +
  facet_wrap(~variable) +
  scale_fill_gradient(low = "yellow", high = "red") +
  geom_polygon(data=base_world[base_world$order,],
               aes(x=long, y=lat, group = group), 
               lwd = 0.01, fill = "grey", inherit.aes = F) + #Put original map in
  geom_polygon() + #Draw data on top
  geom_path(colour = "black", lwd = 0.05) +
  coord_equal() +
  ggtitle("Now it looks a little better") +
  labs(fill = "amount") +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank())

enter image description here