Skip to content

Adding Two-Headed Arrow in map legend

19 messages · Miluji Sb, David Winsemius, Jim Lemon

#
I am trying to draw maps for the world using:

library(rworldmap)
library(maptools)
library(RColorBrewer)


tmp2<- dput(head(pece,10))
structure(list(iso3 = c("AUS", "AUT", "BEL", "CAN", "CHE", "CHL",
"CZE", "DEU", "DNK", "ESP"), eps_score = c(0.877343773841858,
2.68984365463257, 1.31406247615814, 1.98046875, 2.61666655540466,
NA, 1.44414067268372, 2.34257817268372, 2.89687490463257, 2.15937495231628
), gov_eff = c(1.76499999562899, 1.85666667421659, 1.74500000476837,
1.88416666785876, 1.99181815710935, 1.21499997377396, 0.865833342075348,
1.64999999602636, 2.15416664878527, 1.36833332975705), sh_va_enint =
c(13.4375638961792,
8.90904521942139, 10.368335723877, 14.0469560623169, NA, NA,
13.5679216384888, 9.67090892791748, 10.5978908538818, 8.34146690368652
), rd_in_va = c(2.17547988891602, 2.47147130966187, 2.53955459594727,
2.01138758659363, NA, NA, 1.49587619304657, 2.72330951690674,
2.5316367149353, 1.48551619052887)), datalabel = "", time.stamp = " 9 Mar
2016 17:43", .Names = c("iso3",
"eps_score", "gov_eff", "sh_va_enint", "rd_in_va"), formats = c("%9s",
"%8.0g", "%10.0g", "%9.0g", "%9.0g"), types = c(6L, 254L, 255L,
254L, 254L), val.labels = c("", "", "", "", ""), var.labels = c("",
"(mean) eps_score", "(mean) gov_eff", "(mean) sh_va_enint", "(mean)
rd_in_va"
), expansion.fields = list(c("_dta", "ReS_i", "countrycode"),
    c("_dta", "ReS_ver", "v.2"), c("_dta", "ReS_j", "year"),
    c("_dta", "ReS_str", "0"), c("_dta", "ReS_Xij", "a_"), c("_dta",
    "__JVarLab", "ACT"), c("_dta", "__XijVarLabrdd_", "(sum) rdd"
    ), c("_dta", "__XijVarLabp", "Value"), c("_dta", "__XijVarLabpop",
    "Population"), c("_dta", "__XijVarLabest_lu_f", "Source of lu"
    ), c("_dta", "__XijVarLablu", "Percentage of No Schooling"
    ), c("_dta", "__XijVarLabest_lp_f", "Source of lp"), c("_dta",
    "__XijVarLablp", "Percentage of Primary"), c("_dta", "__XijVarLablh",
    "Percentage of Tertiary"), c("_dta", "__XijVarLabest_lh_f",
    "Source of lh"), c("_dta", "__XijVarLabls", "Percentage of Secondary"
    ), c("_dta", "__XijVarLabest_ls_f", "Source of ls"), c("_dta",
    "__XijVarLabvalue", "Value"), c("_dta", "_TStvar", "year"
    ), c("_dta", "_TSpanel", "id2"), c("_dta", "_TSdelta",
"+1.0000000000000X+000"
    ), c("_dta", "_TSitrvl", "1"), c("_dta", "tis", "year"),
    c("_dta", "iis", "id2")), version = 12L, row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "data.frame")
n <- joinCountryData2Map(pece, joinCode="ISO3", nameJoinColumn="iso3")
n <- n[-which(row.names(n)=='Antarctica'),]

# EPS
colourPalette <- rev(brewer.pal(7, "RdYlGn"))

eps <- mapCountryData(n, nameColumnToPlot="eps_score", mapTitle="EPS
Score",colourPalette=colourPalette,
                      catMethod="fixedWidth", missingCountryCol = "white",
addLegend=FALSE)
do.call(addMapLegend, c(eps, legendLabels="all", legendWidth=0.5))

Instead of adding numeric based legend, I would like to add a two-headed
arrow with some text. I would be grateful for any help. Thank you!

Sincerely,

Milu
#
Hi Miluji,
Try this:

arrows(-100,-140,100,-140,code=3)

Jim
On Fri, Apr 8, 2016 at 10:24 PM, Miluji Sb <milujisb at gmail.com> wrote:
#
Forgot to copy the list

Dear Jim,

Thank you for your reply. I must be doing something wrong, If this is my
command to plot a map of Europe:

eps_europe <- mapCountryData(n, nameColumnToPlot="eps_score", mapTitle="EPS
Score - Europe",colourPalette=colourPalette,
                             catMethod="fixedWidth", missingCountryCol =
"white", mapRegion="Europe", addLegend=FALSE)

The following command does not seem to add the arrow. What am I doing wrong?

do.call(addMapLegend, c(eps_europe, legendLabels="none",
arrows(-100,-140,100,-140,code=3)))

Thank you again. I really appreciate it.

Sincerely,

Milu
On Sat, Apr 9, 2016 at 12:20 PM, Jim Lemon <drjimlemon at gmail.com> wrote:

            

  
  
#
Your earlier question had a full world map. That was the context for Jim's reply, which did plot a two headed arrow above the legend in your earlier question. Now you have restricted the plot region to Europe so the coordinates of -100,-140,100,-140 no longer are on the visible plot area. You need to decide where you want the arrows using sensible coordinates.
#
Dear David,

Thank you for your answer. Sorry for the embarrassing mistake. However,
even with when I generate a map for the whole world using:

 eps <- mapCountryData(n, nameColumnToPlot="eps_score", mapTitle="EPS
Score",colourPalette=colourPalette,
                      catMethod="fixedWidth", missingCountryCol = "white",
addLegend=FALSE)

And then use:

do.call(addMapLegend, c(eps, legendLabels="none",
arrows(-100,-140,100,-140,code=3)))

Only a legend with the colours is generated, no arrows. My session info is
below. Thanks again!

R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.2 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] countrycode_0.18   ggplot2_2.1.0      RColorBrewer_1.1-2 foreign_0.8-66
    maptools_0.8-39    rworldmap_1.3-6
[7] sp_1.2-0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.4      lattice_0.20-33  grid_3.2.4       plyr_1.8.3
gtable_0.2.0     spam_1.3-0
 [7] scales_0.4.0     tools_3.2.4      munsell_0.4.3    maps_3.1.0
fields_8.3-6     colorspace_1.2-6

On Sat, Apr 9, 2016 at 7:34 PM, David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
#
I do get an arrow using same version of R and OSX. See attached. (I think that png images will be accepted by the mailserver.)
#
Nope I was wrong, but the copy to Milugi did arrive.

Here's a pdf:
#
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Rplot.pdf
Type: application/pdf
Size: 115266 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160409/bd2b60f1/attachment.pdf>
-------------- next part --------------
David Winsemius
Alameda, CA, USA
#
Hello David,

This is exactly what I want but I still can't get the arrows. R and R
studio is updated. Thanks again!

Sincerely,

Milu

On Sat, Apr 9, 2016 at 10:29 PM, David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
#
I didn't try it in Rstudio until just now (and I don't remember that you ever mentioned RStudio as a possible issue.) The plotting I see in the default graphics Rstudio window is rather different than what I see in the default plotting window for the R.app GUI. I'm guessing you are reporting results from viewing plots in that IDE's viewing window.

I would try plotting with the png() or pdf() devices and see if the results are more predictable. I just tried with pdf() from RStudio and the results were much closer to what I was seeing with saving from R.app. The `quartz()` device seems to deliver consistent results for me. The RStudio device is something they call RStudioGD, and I don't have sufficient experience to explain its quirks.
#
Dear David,

The device was the issue. The quartz() device works fine but pdf() does
not. Now I just need to figure out the limits for map for Europe. Thanks
for all your help and patience.

Sincerely,

Milu

On Sun, Apr 10, 2016 at 7:10 PM, David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
#
After plotting a map of Europe with base graphics the coordinates of the lower-left and upper-right corners are obtained by par('usr')
[1] -12.20000  47.20000  25.89375  79.10625
#
Dear David,

Thank you very much for your replies! I didn't know about par('usr').

I get different coordinates though:

[1] -19.75966  54.75966  33.60000  71.40000

But the arrow is not at the bottom of the map. I will keep playing with
this. Thanks again!

Sincerely,

Milu

On Mon, Apr 11, 2016 at 12:00 AM, David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
#
Hi Milu,
I just realized that by "the bottom of the map" you may mean "beneath
the map", in which case you should use:

par(xpd=TRUE)
arrows(...)
par(xpd=FALSE)

Jim
On Mon, Apr 11, 2016 at 11:50 PM, Miluji Sb <milujisb at gmail.com> wrote:
#
Dear Jim,

Thanks again! I do want the arrows at the bottom (beneath the map). This is
what I am doing:

# Draw the map
eps_europe <- mapCountryData(n, nameColumnToPlot="eps_score", mapTitle="EPS
Score - Europe",colourPalette=colourPalette,
catMethod="fixedWidth", missingCountryCol = "white", mapRegion="Europe",
addLegend=FALSE)

# ISO3 codes on the map
text(n, labels="ISO3", cex=0.30)

# Obtain coordinates for the arrow
par('usr')

# -19.75966  54.75966  33.60000  71.40000

# Arrows
par(xpd=TRUE)
arrows(-19.75966,  54.75966,  33.60000,  71.40000,code=3)
par(xpd=FALSE)

As the output shows I cannot seem to get the correct coordinates for the
arrows. Thanks again.

Sincerely,

Milu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: myplot.pdf
Type: application/pdf
Size: 116608 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160412/d53f8072/attachment.pdf>
#
Hi Milu,
There is a two-headed arrow on the image you sent, and it seems to be
where you specified. Did you want it beneath the map, as:

par(xpd=TRUE)
arrows(-22,54.75,-22,74,code=3)
par(xpd=FALSE)

Jim
On Tue, Apr 12, 2016 at 7:58 PM, Miluji Sb <milujisb at gmail.com> wrote:
#
Hello Jim,

Thanks again. I am getting the two-headed arrow but I cannot seem to get
the coordinates right for the arrow to appear beneath the map. These
coordinates puts the arrow on the left hand side. Thanks again!

Sincerely,

Milu
On Tue, Apr 12, 2016 at 1:15 PM, Jim Lemon <drjimlemon at gmail.com> wrote:

            
-------------- next part --------------
A non-text attachment was scrubbed...
Name: myplot.pdf
Type: application/pdf
Size: 113072 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160412/745a323a/attachment.pdf>
#
Hi Milu,
My fault here. As I don't have the data to make the map and try out my
suggestions I mixed up the x and y coordinates. Try this:

par(xpd=TRUE)
arrows(-19.75966,53,33.60000,53,code=3)
par(xpd=FALSE)

Jim
On Tue, Apr 12, 2016 at 10:11 PM, Miluji Sb <milujisb at gmail.com> wrote:
1 day later
#
Hello Jim,

You're amazing. This is what finally worked:

arrows(-1,19,35.6,19,code=3, xpd=T).

Don't know the coordinates were giving so much trouble. Maybe something to
do with maps in rworldmap. Thanks again!

Sincerely,

Milu
On Wed, Apr 13, 2016 at 6:51 AM, Jim Lemon <drjimlemon at gmail.com> wrote: