Sjiveru
Sjiveru

Reputation: 117

Vertical CJK text on an R plot

Is there any convenient way to get vertical CJK text onto an R plot? Both as labels for points/polygons, and just as part of a block of arbitrary text placed on the plot.

The one potential method I've found so far is to use the ragg package to pass something like the vrt2 OpenType feature to a font so that it uses one of those glyph sets meant for writing vertical text in a horizontal environment, and then rotating it to be correct. Is there an easier way to do it than that?

Upvotes: 2

Views: 44

Answers (1)

user2554330
user2554330

Reputation: 44788

Simply putting newlines between the characters might be enough. For example,

plot(1,1)
text(1,1, adj = -1, labels="日\n本", family = "Arial Unicode MS")

produces this: screenshot

Doing a block of text is more tedious: I think each line will need to be drawn separately. For example,

plot(1,1, type="n")
text(1,1.2, adj = c(-1,1), labels="古\n池\nや", family = "Arial Unicode MS")
text(1,1.2, adj = c(0,1), labels="蛙\n飛\nび\n込\nむ", family = "Arial Unicode MS")
text(1,1.2, adj = c(1,1), labels="水\nの\n音", family = "Arial Unicode MS")

produces this:

screenshot

Edited to add: If the character spacing is unacceptable, you can use the par("lheight") parameter to adjust it. For example,

plot(1,1, type="n")
par(lheight = 0.8)
text(1,1.2, adj = c(-1,1), labels="古\n池\nや", family = "Arial Unicode MS")
text(1,1.2, adj = c(0,1), labels="蛙\n飛\nび\n込\nむ", family = "Arial Unicode MS")
text(1,1.2, adj = c(1,1), labels="水\nの\n音", family = "Arial Unicode MS")

which gives

screenshot

It's fairly easy to automate the insertion of newlines, but I'll leave that to you. What would be hard would be to have variable spacing, e.g to insert small spaces or parentheses.

Upvotes: 0

Related Questions