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


for ggplot figure, want label categories on barplot first word being italicized, while following words non-italicized. want category labels follows:

staphylococcacae (otu 1)

streptococcus (otu 300)

i've found examples using expression() can italicize few category labels, able many different categories. code make plot follows (but data has many more bars 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 appreciated, , let me know if can clarify anything.

you can make vector of expressions, , apply 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:

enter image description here

if have spaces in labels e.g. otu 100, may want substitute tilde space, e.g. otu~100.


Comments