Change geom_text to bold when parse=TRUE

青春壹個敷衍的年華 提交于 2019-12-10 16:24:13

问题


I am annotating faceted plots to include a superscript, yet am having trouble making the text bold. I realise that this has something to do with creating an expression outside the plot call and then specifying parse=TRUE. There is probably quite a simple solution but so far nothing I have tried has worked, including the use of bquote() and bold(). Thanks

library(ggplot2)

data(iris)

rsq<-c(.3,.6,.75)
pos<-c(5,6,7)
Species<-levels(iris$Species)

big_data<-as.data.frame(cbind(pos, rsq))
big_data$Species<-Species

lab <- paste("r^2 == ", round(big_data$rsq,2))

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  facet_wrap(~Species,scales='free_x')+
  geom_point(size=3,show.legend = F) +
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,fontface='bold')


回答1:


you can get r to be bold()

lab <- sprintf("bold(r)^2 == %.2f", big_data$rsq)

but that's about it. From ?plotmath

Note that bold, italic and bolditalic do not apply to symbols, and hence not to the Greek symbols such as mu which are displayed in the symbol font. They also do not apply to numeric constants.

Your best bet for finer typography might be tikzDevice.




回答2:


A little cheat - over plotting the text 3 times with slight increases in size.

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  facet_wrap(~Species,scales='free_x')+
  geom_point(size=3,show.legend = F) +
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,size=4)+
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,size=4.07)+
  geom_text(aes(x=pos,y=1,label=as.character(lab)),parse=TRUE,data=big_data,size=4.08)



来源:https://stackoverflow.com/questions/43728261/change-geom-text-to-bold-when-parse-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!