r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
858 stars 121 forks source link

hyperlinks in text components (e.g. tm_credits) for pdf output #794

Open ar-onos opened 11 months ago

ar-onos commented 11 months ago

I'm trying to insert a textbox on a map with a paragraph but I don't want the lines broken with "\n" in between the text except for a new paragraph. How do I achieve this with tmap, tm_credit or any other method please? Also can I insert a hyperlink in the tm_credit section for a map that will be saved as a pdf?

library(sf) library(tmap)

tm_shape(NLD_muni) + tm_borders("population") + 
tm_credits("It will be great to create a textbox that is 0.2 of the enter map in Width. This box should be able to take a paragraph with many continuous lines without the use of  line breaks using forward slash n in R" ,col="gray45",size=0.3,fontfamily = "sans", fontface = "italic", bg.color =NA,bg.alpha = 0.9, position=c("left","bottom"),align = "left",width = 0.8)

This example shows what I mean under "Map Information" image

mtennekes commented 11 months ago

Good idea with that automatic text wrapping. I have written similar code from the treemap package that can be reused.

mtennekes commented 11 months ago

I'm not sure if hyperlinks can be used. Is this possible in ggplot2? If so, we can use the same method, since they also rely on the grid graphics.

ar-onos commented 11 months ago

Thanks @mtennekes. Could you share the code snippet from treemap to do this please?

ar-onos commented 11 months ago

I'm not sure if hyperlinks can be used. Is this possible in ggplot2? If so, we can use the same method, since they also rely on the grid graphics.

I found this link for ggplot2 that could be useful https://stackoverflow.com/questions/42259826/hyperlinking-text-in-a-ggplot2-visualization

mtennekes commented 11 months ago

Implemented, but testing needed. So please test the following using the GH version:

tm_shape(NLD_muni) + tm_borders("population") + 
    tm_credits("It will be great to create a textbox that is 0.2 of the enter map in Width. This box should be able to take a paragraph with many continuous lines without the use of  line breaks using forward slash n in R" , col="gray45",fontfamily = "sans", fontface = "italic", bg.color =NA,bg.alpha = 0.9, position=c("left","bottom"), just  = "left",width = 15)

Note: in tmap4, the width of the components/legends is different. Rather than relative values (where 1 means the full map width), it is changed to absolute values, namely, lineheights. So the 15 in the example above means 15 lines of text. This may be a bit strange because we are measuring horizontal rather than vertical space, but on the other hand it is similar to the unit "em" in CSS and Latex.

Thanks for the link of the hyperlink in ggplot2. Certainly useful, but a.t.m. this has low priority for me.

ar-onos commented 11 months ago

Thanks, @mtennekes The continuous text withing the box worked. However, the line break (\n) worked sometimes and at other times, it ignored it. I assume it is because it is still in the testing phase. I noticed the legend default is now on the outside which is nice. Will it be possible to have independent controls for the different attributes outside instead of just the attr parameter please?

image

When I add the frame = T parameter, the frame doesn't cover the entire text in the box.

tm_shape(NLD_muni) + tm_polygons("population") + 
  tm_credits("It will be great to create a textbox that is 0.2 of the enter map in Width. This box should be able to take a paragraph with many continuous lines without the use of  line breaks using forward slash n in R.\nHowever, the linebreak isn't working at all times to break the lines in this version." , col="gray45",fontfamily = "sans", fontface = "italic",frame = T, bg.color =NA,bg.alpha = 0.9, position=c("left","bottom"), size=0.55, just  = "left",width = 10)

image

mtennekes commented 11 months ago

Done :-) Please test @ar-onos I'll leave this issue open for the hyperlinks

ar-onos commented 11 months ago

Done :-) Please test @ar-onos I'll leave this issue open for the hyperlinks

Hi @mtennekes This works very well. I've also tried it using numeric positions. I noticed a new command: frame.r. Not sure what it does but I assigned a value to it for testing purposes:

tm_shape(NLD_muni) + tm_polygons("population") + 
  tm_credits("It will be great to create a textbox that is 0.2 of the enter map in Width. This box should be able to take a paragraph with many continuous lines without the use of  line breaks using forward slash n in R.\nHowever, the linebreak isn't working at all times to break the lines in this version." ,
         col="gray45",fontfamily = "sans", fontface = "italic",frame = T,frame.r = 0.1, frame.lwd =0.1,  bg.color =NA,bg.alpha = 0.9, position=c("left","bottom"), size=0.55, just  = "left",width = 17)+ 
  tm_credits("It will be great to create a textbox that is 0.2 of the enter map in Width. This box should be able to take a paragraph with many continuous lines without the use of  line breaks using forward slash n in R.\nHowever, the linebreak isn't working at all times to break the lines in this version." ,
         col="gray15",fontfamily = "sans", fontface = "italic",frame = T,frame.r = 0.5, frame.lwd =0.2,  bg.color =NA,bg.alpha = 0.9, position=c("left","top"), size=0.55, just  = "left",width = 13) + 
 tm_credits("It will be great to create a textbox that is 0.2 of the enter map in Width. This box should be able to take a paragraph with many continuous lines without the use of  line breaks using forward slash n in R.\nHowever, the linebreak isn't working at all times to break the lines in this version." ,
         col="gray45",fontfamily = "sans", fontface = "italic",frame = T,frame.r = 0.1, frame.lwd =0.5,  bg.color =NA,bg.alpha = 0.9, position=c(0.02,0.5), size=0.55, just  = "left",width = 10)

image

mtennekes commented 11 months ago

You're welcome. frame.r is the radius of the rounded frame corners. 0 means no rounding. If you set it to 5 or 10, you'll see what happens.