Reputation: 329
I want to add both country labels with their respective data in tmaps. I know that tm_text allows me to add labels like this (I know the output looks terrible, but is just to illustrate):
data("World")
tm_shape(World)+
tm_borders()+
tm_fill("HPI",
palette = "Blues")+
tm_text("pop_est")
I want each label to be something like this: "country name" ":" "variable". And I tried with paste0:
tm_shape(World)+
tm_borders()+
tm_fill("HPI",
palette = "Blues")+
tm_text(paste0("pop_est", ":", "HPI"))
Error: Incorrect data variable used for the text
Does anybody knows how to do this? Thanks in advance!
Upvotes: 0
Views: 656
Reputation: 4354
you are getting the error because the tm_text is expecting a character input (which then is used as a column name behind the scenes). The problem is that the result of paste() or paste0() is a string itself but but it is not an existing column.
The solution is to generate a new column of the desired text and use this as input for tm_text.
library(tmap)
World$text <- paste0(World$pop_est, ":", World$HPI)
tm_shape(World)+
tm_borders()+
tm_fill("HPI",
palette = "Blues")+
tm_text("text")
As you said, the output is ugly, therefore I will not include it.
Upvotes: 1