How to change fontsize in direct.label?

2020-06-01 10:19发布

问题:

I can't change the fontsize in a direct.label (from the directlabels package) ggplot2 plot. See below for a reproducible example - there's no problem in rotating the labels 45 degrees, making them bold, serif, and 50% transparent (all the other arguments in the list at the end of the code below) - but I can't control the fontsize. (I don't really want them to be 25, this is just for testing....)

Is there something I'm missing, or is this a bug?

library(ggplot2)
library(scales)
library(directlabels)
df <- data.frame(x = rnorm(26), y=rnorm(26), let=letters)
p <- ggplot(df, aes(x, y, color=let)) + geom_point() 
direct.label(p, 
    list("top.points", rot=45, fontsize=25, 
        fontface="bold", fontfamily="serif", alpha=0.5))

回答1:

I figured it out, you use cex to change the font size.

df <- data.frame(x = rnorm(26), y=rnorm(26), let=letters)
p <- ggplot(df, aes(x, y, color=let)) + geom_point() 
direct.label(p, 
    list("top.points", rot=45, cex=6, 
          fontface="bold", fontfamily="serif", alpha=0.5))

That would give you,



回答2:

It's kind of a different route, but would you consider doing it all in ggplot2?

ggplot(df, aes(x, y, color=let)) + 
       geom_point() + 
       geom_text(df, mapping=aes(x, y, label=let, colour=let), 
       size=5, vjust=-.55, hjust=.55, angle = 45, fontface="bold", 
       family ="serif", alpha=0.5) + opts(legend.position = "none")

This would give you this, and you can adjust the fontsize using size