0

I am using ggplot2. I have species names in my legend and axis title. I found a way to italicize the an entire group: theme(legend.position="right", axis.text.y = element_text(face = "italic")) but I need to know how to pick and choose which words to italicize. For example, the axis title needs to be Carollia Species and the legend title Piper Species. Any suggestions?

> head(pie2)
               bat          piper       prop
1      C. castanea P. scintillans 0.51215992
2 C. perspicillata P. scintillans 0.47720417
3       C. sowelli P. scintillans 0.61759343
4      C. castanea P. reticulatum 0.08928571
5 C. perspicillata P. reticulatum 0.04177665
6       C. sowelli P. reticulatum 0.02287988

pie2 %>%
mutate(piper = factor(piper, levels = unique(pie2$piper))) %>%
ggplot(aes(x=bat, y=prop, fill = piper)) +
geom_bar(stat = 'identity', position = 'stack') +
theme_minimal() +
theme(legend.position="bottom")+
scale_fill_brewer(palette=3, direction = -1)+   
coord_flip()+
labs(y='Proportion in Diet', x='Carollia Species')+
guides(fill=guide_legend(title="Piper Species"))+
theme(legend.position="right", axis.text.y = element_text(face = "italic"))

graph

  • [This question](https://stackoverflow.com/questions/32555531/how-to-italicize-part-one-or-two-words-of-an-axis-title) looks closely related. It should work for both the title (as shown in the answer to the question) and for the legend title. – aosmith Apr 06 '18 at 19:24
  • That worked beautifully! Any advice on changing specific words in the legend labels? ex) Non-Piper – Lauren Maynard Apr 06 '18 at 19:38
  • I think it's similar, using expressions for the `labels` in `scale_fill_brewer`. I searched "ggplot2 italics legend labels" and [this](https://stackoverflow.com/questions/48688404/how-to-italicize-1-category-in-a-legend-in-ggplot2) was the second hit. – aosmith Apr 06 '18 at 19:48

0 Answers0