Home > database >  How to convert text into image in R?
How to convert text into image in R?

Time:06-16

I want to display text information in a graphics plot via textplot(), however text is cut. Does anybody know how to make it fit?

library(gplots)

str <- as.character("R is a programming language for statistical computing and graphics supported by the R Core Team and the R Foundation for Statistical Computing. Created by statisticians Ross Ihaka and Robert Gentleman, R is used among data miners, bioinformaticians and statisticians for data analysis and developing statistical software.[6] Users have created packages to augment the functions of the R language.

According to user surveys and studies of scholarly literature databases, R is one of the most commonly used programming languages used in data mining.[7] As of March 2022, R ranks 11th in the TIOBE index, a measure of programming language popularity, in which the language peaked in 8th place in August 2020")

textplot(str, halign = c("right"),
         valign = c("bottom"), cex=0.5, fixed.width=TRUE,
         cspace=1, lspace=1, mar=c(0, 0, 3, 0)   0.1,
         tab.width = 8)

enter image description here

CodePudding user response:

An option is adding \n to your string to create enters and use annotate with ggplot like this:

str <- as.character("R is a programming language for statistical computing and graphics supported by the R Core Team and \n the R Foundation for Statistical Computing. Created by statisticians Ross Ihaka and Robert Gentleman, \n R is used among data miners, bioinformaticians and statisticians for data analysis and developing statistical software.[6] \n Users have created packages to augment the functions of the R language.

According to user surveys and studies of scholarly literature databases, R is one of the most \n commonly used programming languages used in data mining.[7] As of March 2022, R ranks 11th in the TIOBE index, \n a measure of programming language popularity, in which the language peaked in 8th place in August 2020")

library(ggplot2)
ggplot()  
  annotate("text", x = 4, y = 25, size = 3, label = str)  
  theme_void()

Output:

enter image description here

CodePudding user response:

From the documentation:

cex

Character size, see par for details. If unset, the code will attempt to use the largest value which allows the entire object to be displayed.

Therefore it would be better to not set the cex parameter at all like so:

textplot(str, halign = c("right"),
         valign = c("bottom"), fixed.width=TRUE,
         cspace=1, lspace=1, mar=c(0, 0, 3, 0)   0.1,
         tab.width = 8)
  • Related