[This article was first published on r-bloggers on Programming with R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
ggplot2
is go-to R package for anyone who wants to make beautiful static visualizations in R. But most ggplot2 gplots look almost the same and little many data analysts or data scientists care about customizing it, primarily because it’s not very intuitive to do so. That’s where ggplot2 extensions come in very handy. ggtext
is an R package (by Claus O. Wilke) that helps in customizing the text present in ggplot2 plots. It could be the text outside the plot canvas or the text (annotation) within the plot canvas.
Youtube – https://www.youtube.com/watch?v=bb4k6OpFRTo
Video Tutorial
Please subscribe to the channel and leave a feedback if it’s useful. It’ll really good to hear from you!
Code
library(ggtext) library(ggplot2) ggplot(iris) + geom_point(aes(x = Sepal.Length, y = Petal.Length)) + facet_wrap(~ Species) + theme( strip.text = element_textbox( size = 12, color = "white", fill = "red", box.color = "black", halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"), padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3) ) )
library(ggtext) library(ggplot2) ggplot(iris) + geom_point(aes(x = Sepal.Length, y = Petal.Length)) + facet_wrap(~ Species) + theme( strip.text = element_textbox( size = 12, color = "white", fill = "red", box.color = "black", halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"), padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3) ) ) library(ggplot2) library(ggtext) ggplot(iris) + geom_point(aes(x = Sepal.Length, y = Petal.Length)) + labs(title = "**iris analysis** <br/> <i style='color:red; padding-left:-4px'>super</i>") + theme( plot.title = element_textbox_simple( fill = 'yellow', padding = margin(5.5, 5.5, 5.5, 5.5) ) ) ggplot(mtcars, aes(disp, mpg)) + geom_point() + labs( title = "<b>Fuel economy vs. engine displacement</b><br> <span style = '-size:10pt'>Lorem ipsum *dolor sit amet,* consectetur adipiscing elit, **sed do eiusmod tempor incididunt** ut labore et dolore magna aliqua. <span style = 'color:red;'>Ut enim ad minim veniam,</span> quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</span>", x = "displacement (in<sup>3</sup>)", y = "Miles per gallon (mpg)<br><span style = '-size:8pt'>A measure of the car's fuel efficiency.</span>" ) + theme( plot.title.position = "plot", plot.title = element_textbox_simple( size = 13, lineheight = 1, padding = margin(5.5, 5.5, 5.5, 5.5), margin = margin(0, 0, 5.5, 0), fill = "cornsilk" ), axis.title.x = element_textbox_simple( width = NULL, padding = margin(4, 4, 4, 4), margin = margin(4, 0, 0, 0), linetype = 1, r = grid::unit(8, "pt"), fill = "azure1" ), axis.title.y = element_textbox_simple( hjust = 0, orientation = "left-rotated", minwidth = unit(1, "in"), maxwidth = unit(2, "in"), padding = margin(4, 4, 2, 4), margin = margin(0, 0, 2, 0), fill = "lightsteelblue1" ) )
References
To leave a comment for the author, please follow the link and comment on their blog: r-bloggers on Programming with R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.