0

I'm trying to use the typed content of a vector as title for a graph. Therefore (i think) i first need to convert the typed content to characters. And here it gets tricky....

I already tried to convert the content to "as.character()", "format()" etc...but in all cases i get the output of the dataframe itself en not just the content of the typen vector.

For example, say have I have 2 vectors...test_a and test_b

test_a <- read.csv2("test.csv", sep = ";", dec = ",")
test_b <- read_excel("test.xlsx")

x <- test_a ##(or test_b)
y <- as.character(x) ##this doesn't work :(


fig <- ggplot(x, aes(x = OUTCOME)) 
fig + geom_histogram(aes(color = SEX), 
                        breaks=seq(0, 1500, by = 1),
                        alpha = 1) +
        ggtitle(as.character(y))

As title of the graph I expect to get "test_a" or "test_b" as in "y" and not the numbers in test_a or test_b...Your help is appreciated a lot..

Andrei
  • 3
  • 3

1 Answers1

2

We can use substitute and eval here

x <- substitute(iris)

ggplot(eval(x), aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  labs(title = x)

enter image description here

markus
  • 24,556
  • 5
  • 34
  • 51