1

我正在尝试使用 ggplot2 在地图内绘制子图。已经有一个问题处理了这个问题,但我相信这个问题的不同之处在于子图与地图信息没有太大关系。要解决问题,请运行代码直到问题。

library(ggplot2)
library(plyr)
library(reshape2)

获取地图信息

con <- url("http://gadm.org/data/rda/ARG_adm2.RData")
print(load(con))
close(con)

ggmap <- fortify(gadm, region = "NAME_1") 

清除背景(现在运行这两个,稍后将使用)

theme_martin <- 
  theme(
    axis.line=element_blank(),
    axis.text.x=element_blank(),
    axis.text.y=element_blank(),
    axis.ticks=element_blank(),
    axis.ticks.length=unit(0.3, "lines"),
    axis.ticks.margin=unit(0.5, "lines"),
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    legend.background=element_rect(fill="white", colour=NA),
    legend.key=element_rect(colour="white"),
    legend.key.size=unit(1.2, "lines"),
    legend.position= "bottom",
    legend.text=element_text(size=rel(0.8)),
    legend.title=element_text(size=rel(0.8), face="bold", hjust=0),
    panel.background=element_blank(),
    panel.border=element_blank(),
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank(),
    panel.margin=unit(0, "lines"),
    plot.background=element_blank(),
    plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
    plot.title=element_text(size=rel(1.2)),
    strip.background=element_rect(fill="grey90", colour="grey50"),
    strip.text.x=element_text(size=rel(0.8)),
    strip.text.y=element_text(size=rel(0.8), angle=-90) 
  )


theme_martin_2 <- 
  theme(
    axis.text.y=element_blank(),
    axis.ticks=element_blank(),
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    legend.background=element_rect(fill="white", colour=NA),
    legend.key=element_rect(colour="white"),
    legend.key.size=unit(1.2, "lines"),
    legend.position="right",
    legend.text=element_text(size=rel(0.8)),
    legend.title=element_text(size=rel(0.8), face="bold", hjust=0),
    panel.background=element_blank(),
    panel.border=element_blank(),
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank(),
    panel.margin=unit(0, "lines"),
    plot.background=element_blank(),
    plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
    plot.title=element_text(size=rel(1.2)),
    strip.background=element_rect(fill="grey90", colour="grey50"),
    strip.text.x=element_text(size=rel(0.8)),
    strip.text.y=element_text(size=rel(0.8), angle=-90) 
  )

地块地图

p <- ggplot(data=ggmap, aes(x=long, y=lat)) 
p <- p + geom_polygon(data=ggmap, colour="white", fill = "#00BFC4", aes(group=group))
p <- p + coord_map(xlim=-c(77, 53), ylim=-c(56,20))
p <- p + theme_martin
p

我的数据框

df_wide <- data.frame(PROV = c("BUENOS AIRES", "TUCUMAN", "SANTA CRUZ"), 
                      Var1 = c(15, 12, 8), Var2 = c(20, 10, 15), Var3 = c(5, 7, 8),
                      long = c(-32.971804, -28.459033, -43.452919), 
                      lat = c(-54.056168,-64.714966,-67.175903))

df_long <- melt(df_wide[,1:4])
df_long <- join(df_long, df_wide, type = "inner")
df_long <- df_long[,c(1:3, 7:8)]

df_1 <- subset(df_long, PROV == "BUENOS AIRES")
df_2 <- subset(df_long, PROV == "TUCUMAN")
df_3 <- subset(df_long, PROV == "SANTA CRUZ")

col_bar <- c("#00BA38", "#00BFC4", "#D7BA00")

问题

如何使用 ggplot 或类似以下 (sub_plot_i) 的基本图形将条形图添加到该图中?我想将它们添加到每个数据框中表示的地理坐标:例如:位于 BUENOS AIRES 条形图位于df_1[1,4:5] TUCUMAN 条形图位于df_2[1,4:5] SANTA CRUZ 条形图位于df_3[1,4:5]

sub_plot_1 <- ggplot(df_1, aes(x = factor(variable), y = value))
sub_plot_1 <- sub_plot_1 + geom_bar(data=df_1, stat = "identity", fill = col_bar) 
sub_plot_1 <- sub_plot_1 + geom_text(aes(label=value), colour= col_bar, size = 7, vjust = -1)
sub_plot_1 <- sub_plot_1 + theme_martin_2

sub_plot_2 <- ggplot(df_2, aes(x = factor(variable), y = value))
sub_plot_2 <- sub_plot_2 + geom_bar(data=df_2, stat = "identity", fill = col_bar) 
sub_plot_2 <- sub_plot_2 + geom_text(aes(label=value), colour= col_bar, size = 7, vjust = -1)
sub_plot_2 <- sub_plot_2 + theme_martin_2

sub_plot_3 <- ggplot(df_3, aes(x = factor(variable), y = value))
sub_plot_3 <- sub_plot_3 + geom_bar(data=df_2, stat = "identity", fill = col_bar) 
sub_plot_3 <- sub_plot_3 + geom_text(aes(label=value), colour= col_bar, size = 7, vjust = -1)
sub_plot_3 <- sub_plot_3 + theme_martin_2

print(sub_plot_1)
print(sub_plot_2)
print(sub_plot_3)

我已经尝试过使用 ggplot2 在地图上绘制条形图的建议?问题,但他们似乎没有处理我尝试过 ggsubplot() 的相同问题,但效果不佳。我也尝试过将两个图一起打印,但也没有工作。

我遇到的问题以及为什么我认为这个问题与链接问题不同的是,条形图与地图信息无关。这些是几乎来自外部信息的条形图,唯一连接地图和条形图的是条形图的位置。

4

0 回答 0