1

I´m attempting to plot sub-plots inside a map using ggplot2. There is a question that deals with this issue already, but the difference i believe this one has it that the sub-plots don´t have much to do with the map information. To follow throught the question, please run the code until question.

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

Get Map info

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

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

Clear Background (run these two now, will be used later)

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) 
  )

Plot Map

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

My data frames

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")

Question

How can I add barplots using ggplot or base graphics like the following (sub_plot_i)to this plot? I would like to add them at the geographical coordinates denoted in each data frame: For example: The BUENOS AIRES barplot located at df_1[1,4:5] The TUCUMAN barplot located at df_2[1,4:5] The SANTA CRUZ barplot located at 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)

I´ve tried the suggestions in this Plotting bar charts on map using ggplot2? question but they don´t seem to be dealing with the same problem I´ve tried ggsubplot() but it didn´t work so well. I´ve also tried printing both plots together but didn´t work either.

The problem I have and why i believe this question is different than the link question is that the Barplots don´t to do with the map information. These are barplots that come from almost external information and the only thing conecting the map and the barplots is the location of the barplots.

Community
  • 1
  • 1
marbel
  • 7,560
  • 6
  • 49
  • 68
  • 2
    If you don't use `coord_map`, you can use `annotation_custom` – mnel Aug 02 '13 at 03:23
  • 1
    Yes, you can easily use `annotation_custom` but be careful http://stackoverflow.com/questions/17780886/draw-several-plots-at-specified-coordinates-using-annotation-custom – DrDom Aug 02 '13 at 04:12
  • 2
    the ggmap package defines its own version of annotation_custom (called `inset`); hopefully with a better implementation – baptiste Aug 02 '13 at 12:48

0 Answers0