Aveshen Pillay
Aveshen Pillay

Reputation: 481

Format of key label text in legend in ggplot

I would like to edit the format of the key labels and title in a legend with log transformed data.

I am using ggplot and calling scale_fill_distiller with trans = “log10”:

breaks_vec = c(1, 10, 100, 1000, 10000, 100000)
labels_vec = c(1, 10, 100, 1000, 10000, 100000)

scale_fill_distiller(palette = "Spectral", trans = "log10", breaks = breaks_vec, labels = labels_vec)

I would like change the text format of the key labels to 1, 10, 100, 1000, 10^4, 10^5. I would like to have the last two labels without the "^" character, but rather the numbers 4 and 5 as superscript.

I would also like to change the legend title to “Confirmed (log10)” with the log10 formatted with the "10" as a subscript.

Currently, the legend format looks as follows:

enter image description here

Upvotes: 0

Views: 558

Answers (1)

Edward
Edward

Reputation: 19209

Try the following:

scale_fill_distiller(name=expression(Confirmed~(log[10])), 
palette = "Spectral", trans = "log10", 
breaks = breaks_vec, 
labels = parse(text=c("1","10","100","1000","10^4","10^5"))) +
guides(fill=guide_legend(title=expression(Confirmed~(log[10]))))

enter image description here

Upvotes: 1

Related Questions