Good cex-adjusted rotated text labels in R, especially at srt=180

Viewed 89

I have been struggling with placing rotated text labels how I want them to be, so I finally decided to create a reference plot for quick intution: enter image description here

code included below. This plot illustrates some of the sensitivities with respect to font sizes.

What I have not managed to do is to create a right-justified "Angle is 180" that leaves a modest amount of space to the anchor point. (You can think of this as keeping the location and rotating the text by 180 degrees.) For that matter, the 135 angle and 225 degree angles also should be rotated 180 degrees. If someone knows how to do this, I would love to know.

wanted circular text arrangement


iaw$hline <- function (yloc, xloc = c(-9e99,9e99), ...)
  for (i in 1:length(yloc)) lines(xloc, c(yloc[i], yloc[i]), ...)

iaw$vline <- function (xloc, yloc = c(-9e99,9e99), ...) 
  for (i in 1:length(xloc)) lines(c(xloc[i], xloc[i]), yloc, ...)


angles <- (0:5)*45

S <- 150

png(file = "textlabels.png", width = 9 * S, height = 6 * S, pointsize = 24 )
#pdf(file = "textlabels.pdf", width = 9, height = 6, pointsize = 12 )

plot( 0, ylim=c(0,100), xlim=c(0,100), xlab="", ylab="", type="n" )

iaw$vline( c(10,30,40,55,75,95), col="gray", lwd=0.5 )
iaw$hline( c(10,30,60,85), col="gray", lwd=0.5 )

################

x <- 10

y <- 20
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a )
text( x, y - 20, "A. cex=1", cex=0.5, col="blue" )

y <- 60
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3 )
text( x, 5+y - 10, "B. srt=a", cex=0.5, col="blue" )

################

x <- 30

y <- 10
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, pos=1 )
text( x, y - 10, "C. pos=1", cex=0.5, col="blue" )

y <- 60
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, pos=2 )
text( x, y - 10, "D. pos=2", cex=0.5, col="blue" )

y <- 30
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, pos=3 )
text( x, 5+y - 10, "E. pos=3", cex=0.5, col="blue" )

y <- 85
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, pos=4 )
text( x, y - 10, "F. pos=4", cex=0.5, col="blue" )


x <- 40
y <- 10
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, adj=c(-2,3), pos=1 )
text( x, y - 10, "G. pos=1\nadj=(-2,3)", cex=0.5, col="blue" )

text( x, 15, "(pos overrides adj)", cex=0.5, col="blue")


################

x <- 55

y <- 30
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3/2, adj=c(-0.5,0.5) )
text( x, 3+y - 10, "H. adj=(-0.5,0.5)\ncex=0.15", cex=0.5, col="blue" )

y <- 10
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, adj=c(-0.5,0.5) )
text( x, y - 10, "I. adj=(-0.5,+0.5)", cex=0.5, col="blue" )


y <- 60
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, adj=c(1,0) )
text( 1+x, y - 10, "J. adj=(1,0)", cex=0.5, col="blue" )


y <- 85
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, adj=c(-0.0,0.0) )
text( x, y - 10, "K. adj=(-0.0,+0.0)", cex=0.5, col="blue" )


################


x <- 75

y <- 10; x <- 85;
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=1.0, adj=c(-0.5,0.5) )
text( 5+ x, y - 5, "L. adj=(-0.5,+0.5)\ncex=1", cex=0.5, col="blue" )
x <- 75


y <- 60
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, adj=c(1,1) )
text( x, y - 10, "M. adj=(1,1)", cex=0.5, col="blue" )


y <- 85
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, adj=c(0,1) )
text( x, 3+ y - 10, "N. adj=(0,1)", cex=0.5, col="blue" )


################

rect( 88,47,103,72, border="red" )

x <- 95
y <- 60
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.4, adj=c(-0.25,0.5) )
text( x, y - 10, "O. adj=(-0.25,0.5)", cex=0.5, col="blue" )



y <- 85
points( x, y, col="blue", pch=20 )
for (a in angles) text( x, y, paste0("Angle is ", a), srt=a, cex=0.3, adj=c(-0.5,0.5) )
text( x, y - 10, "P. adj=(-0.5,+0.5)", cex=0.5, col="blue" )

text(55, 38, "Distance between Point and Text\nis Font-Size Dependent", col="blue", cex=0.5)


################

dev.off()
0 Answers
Related