31387

R ggplot2 Using Italics and Non-Italics in the Same Category Label

For my ggplot figure, I want to label categories on a barplot with the first word being italicized, while the following words are non-italicized. I want the category labels to look like follows:

Staphylococcacae (OTU 1)

Streptococcus (OTU 300)

I've found examples using expression() where I can italicize a few category labels, but I would like to be able to do this for many different categories. The code to make a plot is as follows (but my data has many more bars to plot).

tmp.data <- data.frame(bactnames=c("Staphylococcaceae","Moraxella","Streptococcus","Acinetobacter"),OTUname=c("OTU_1","OTU_2","OTU_3","OTU_4"),value=c(-0.5,0.5,2,3)) tmp.data$bactnames2 <- paste0(tmp.data$bactnames," (",tmp.data$OTUname,")") tmp.data$finalnames <- factor(tmp.data$bactnames2,levels=tmp.data$bactnames2[order(tmp.data$value)],ordered=TRUE) ggplot(tmp.data, aes(finalnames,value)) + geom_bar(stat="identity") + coord_flip()

Any thoughts would be appreciated, and let me know if I can clarify anything.

Answer1:

You can make a vector of expressions, and apply it to the labels argument in scale_x_discrete:

labs <- sapply(strsplit(as.character(tmp.data$finalnames), " "), function(x) { parse(text = paste0("italic('", x[1], "')~", x[2])) }) ggplot(tmp.data, aes(finalnames,value)) + geom_bar(stat="identity") + coord_flip() + scale_x_discrete(labels = labs)

Output:

<img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/16HWw.png" alt="enter image description here">

If you have spaces in your labels e.g. OTU 100, you may want to substitute a tilde for the space, e.g. OTU~100.

Recommend

  • Transparent Overlapping Circular Progress Bars (Custom Control)
  • Need help lining up x axis ticks with bars in D3.js bar chart
  • Understanding how “reorder” in R works
  • Plotting series histogram in Pandas
  • Disable right click menu of shapes
  • d3.js noob : How to pass dynamic variable as data
  • Create histogram (not barplot) from categorical variable
  • Flot - bar chart with individual colors and labels
  • Linq to XML query works when returning single entity but needs to be of list
  • R ggplot2 Using Italics and Non-Italics in the Same Category Label
  • midpoints returned by barplot function do not actually line up with midpoints of bars
  • Is it possible to set a direction for the Kendo DatePicker (Calendar)?
  • geom_bar width in ggplot2 [duplicate]
  • C# - Force Excel to ReCalc before Save
  • Invoke Ribbon button in Outlook 2013
  • Postgresql left outer join on json array
  • How to produce a Code 39 that can be reliably read after faxing
  • Matplotlib: Custom colormap with three colors [duplicate]
  • How to deal with SpiderWebPlot in JFreeChart?
  • Cythonized function unexpectedly slow
  • Passing parameter through “window.location.href”
  • Highcharts - Column chart with empty columns for date in x-axis
  • Geom_jitter colour based on values
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Linq Merge lists
  • wxPython: displaying multiple widgets in same frame
  • Paperclip, set path outside of rails root folder
  • R - Combining Columns to String Based on Logical Match
  • Shallow update not allowed (git > 1.9)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal