Home > database >  ggplot only change the font of certain parts in the legend
ggplot only change the font of certain parts in the legend

Time:07-01

Is it possible to put the scientific names of the fish in italics on the x-axis and in the legend and to use normal font for the rest?

In my case I would like that for example Barbatula barbatula (Bachschmerle) only Barbatula barbatula is in italics and (Bachscmerle) in the normal font

This is the bar chart right now

And this is a part of the data im using

My code is:

ggplot(R_Sandbach, aes(x = fct_infreq (Species), fill=Species )) 
  geom_bar() 
  theme_minimal() 
  geom_text(aes(label=..count..), stat = "count", vjust = -.1, colour= "black") 
  theme(axis.text.x = element_text(angle = 90, size = 5)) 
  xlab("Fischarten") 
  ylab("Individuenanzahl")

CodePudding user response:

ggtext is definitely a good option, although you can just manipulate aspects of your plot directly using theme as you've done in your question.

See an example below:

library(tidyverse)

ggplot()  
  geom_blank()  
  labs(x = "Fischarten", y = "Individuenanzahl", title = "My Super Awesome Title")  
  theme(axis.title.x  = element_text(face = "italic"),
        plot.title = element_text(face = "italic"))


This produces a blank plot with the plot title and x axis both in italics. You can add as many changes into your theme() function as you like.

EDIT:

In the case of which you need specific words of your titles in italics, etc. you can manually incorporate html into your text and then format your plot to read this html.

In my personal experience this has sometimes caused issues with the general themes of ggplot.

Using this package library(mdthemes) solves the issues of html not being read as it should.

For example:

ggplot()  
  geom_blank()  
  labs(x = paste("<i>Fischarten</i>", "Other Title Stuff"), y = "Individuenanzahl", title = paste("My Super", "<i>Awesome Title</i>"))  
mdthemes::md_theme_classic()

  • Related