Exponent numbers in ggplot labels

Hi,

I'm new to R and I need your help. ChatGPT and Bard didn't help me so far, so I'm trying my luck here.

It's about the following code:

ggplot() + geom_line(data = GoeW_EK_daily, aes(x = TIMESTAMP, y = ET_f, color = "Wald"), linewidth = 1) + geom_line(data = FBG_EK_daily, aes(x = TIMESTAMP, y = ET_f, color = "Wiese"), linewidth = 1) + labs(x = "Zeit", y = expression(paste("\u03BCmol m^-2 s^-1" )), color = "Standort") + scale_color_manual(values = c("Wald" = "red", "Wiese" = "blue")) + theme_light() + scale_x_date(date_breaks = "2 week", date_labels = "%d.%m.%Y")

I want to display the -2 and -1 as exponents in the line labs. I tried a few versions, but none of them could display both numbers at the same time. Do you have any idea how this can work?

Thanks for your help!

Welcome to the community @Boltimar! The ggtext package is helpful for adding superscipts via HTML. Below is an example using the mtcars dataset with your y-axis title.

library(tidyverse)
library(ggtext)

ggplot(mtcars) +
  geom_point(aes(x = mpg, y = disp)) +
  labs(x = "Zeit", 
       y = "\u03BCmol m<sup>-2</sup> s<sup>-1</sup>", 
       color = "Standort") +
  theme(axis.title.y = element_textbox_simple(width = NULL,
                                              orientation = 'left-rotated'))

Created on 2023-08-18 with reprex v2.0.2

2 Likes

Hi @Boltimar , remember put a reproducible example of data an example is there:
Reproducible example of data

With a toy data, I'm suggest you this:

GoeW_EK_daily <- data.frame(
  TIMESTAMP = seq(as.Date("2023-01-01"), as.Date("2023-01-15"), by = "days"),
  ET_f = c(5, 7, 8, 6, 4, 9, 10, 8, 6, 5, 7, 8, 6, 4, 9))

FBG_EK_daily <- data.frame(
  TIMESTAMP = seq(as.Date("2023-01-01"), as.Date("2023-01-15"), by = "days"),
  ET_f = c(3, 5, 6, 4, 2, 7, 8, 6, 4, 3, 5, 6, 4, 2, 7))

ggplot() +
  geom_line(data = GoeW_EK_daily, aes(x = TIMESTAMP, y = ET_f, color = "Wald"), linewidth = 1) +
  geom_line(data = FBG_EK_daily, aes(x = TIMESTAMP, y = ET_f, color = "Wiese"), linewidth = 1) +
  labs(x = "Zeit", 
       y = expression(paste("\u03BCmol m"^"-2","s"^"-1")),  # put in  " " the text
       color = "Standort") +
  scale_color_manual(values = c("Wald" = "red", "Wiese" = "blue")) +
  theme_light() +
  scale_x_date(date_breaks = "2 week", date_labels = "%d.%m.%Y")

2 Likes

Hey there,

thanks for your help.It works now. For the next time i will put a reproducable example of data to my issue.

Thank you so much! :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.