Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to italicize part (one or two words) of an axis title

Tags:

r

ggplot2

axis

Is there any way to change the style of part of an axis title while keep the rest part unchanged? In my case, How could I italicize
"bacteria X" in the y-axis title? To my knowledge, the command theme(axis.title.y=element_text(face="italic")) can only change the whole y-aixs title, is it?

ggplot(fig1,aes(x=cf,y=Freq,fill=Var1)) + geom_bar(stat="identity") + labs(x="Groups",y="No. of bacteria X isolates with corresponding types",fill="Var1") + theme(axis.title.y=element_text(face="italic")) 
like image 280
eze Avatar asked Sep 13 '15 23:09

eze


People also ask

How do you italicize parts of text?

Use the <em> tag. The “em” in <em> literally stands for emphasis. Browsers will, by default, make italicize text that is wrapped in HTML <em> tags. Imagine the sound of that sentence, where the reader is emphasizing that word giving the sentence a different feel that if they didn't.

How do you italicize words in R?

To write text in italic font, use a single underscore or asterix before and after the text. To write text in bold font, use a double asterix or underscores before and after the text.

How do you italicize in ggplot2?

Example 2: Adding an italic Text to ggplot2 Plot. In this example, firstly we will be creating a data frame of 10 elements and plotting it with the help of ggplot() function in the ggplot2 library. And then with the annotate() function and passing the italic parameter in the fontface, we will add italic text to it.

Should axis labels be bold?

How can I format the axis title and axis labels separately? (e.g. one bold and one not) Tableau Community (Employee) asked a question. For work, I need to follow standard style guidelines. One of these is that the title of the axis should be in bold, but the axis labels/numbers should not be.


2 Answers

You could make an expression like this:

my_y_title <- expression(paste("No. of ", italic("bacteria X"), " isolates with corresponding types")) .... + labs(y=my_y_title) 
like image 66
Heroka Avatar answered Sep 23 '22 02:09

Heroka


This can be achieved using element_markdown() from the ggtext package.

ggplot(fig1, aes(cf, Freq, fill = Var1)) +   geom_bar(stat = "identity") +   labs(     x = "Groups",     y = "No. of *bacteria X* isolates with corresponding types",     fill = "Var1"   ) +   theme(axis.title.y = ggtext::element_markdown()) 

Notice the * around bacteria X in the y axis title. Setting axis.title.y to element_markdown has the effect that the axis title is rendered as markdown. Thus, text inside * will be displayed in italics.

An even easier solution is using the mdthemes package which provides themes that interpret text as makrdown out of the box, i.e. no need to call theme. Here's an example.

ggplot(mtcars, aes(hp, mpg)) +   geom_point() +   mdthemes::md_theme_classic() +   labs(title = "**Bold Title**", x = "*Italics axis label*") 

enter image description here

like image 21
Thomas Neitmann Avatar answered Sep 21 '22 02:09

Thomas Neitmann