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
  • `y<-as.character` not `as.chracter` – NelsonGon Jan 27 '19 at 15:39
  • What you're doing is assigning test_a to a new object x. This doesnot bring the name to ggtitle but brings some values from test_a. – NelsonGon Jan 27 '19 at 15:46
  • I just need the text after "<-" as title....so no data from the dataframe. :) – Andrei Jan 27 '19 at 15:47
  • @NelsonGon: thnx for the typo comment :p....The reason I assign test_a to x is that i have test_a to test_m...and after that there is a large chunck of code where x is returning. – Andrei Jan 27 '19 at 15:50
  • Please share the output of `str(test_a)`. – NelsonGon Jan 27 '19 at 15:52
  • @markus i just want to use the text "test_a" and not the values from test_a... – Andrei Jan 27 '19 at 15:53
  • @NelsonGon: output of str(x) Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 27916 obs. of 44 variables; str(test_a) is the Same ;) – Andrei Jan 27 '19 at 15:55
  • From your comment above I thought test_a was a data.frame of data.frames. – NelsonGon Jan 27 '19 at 15:59
  • I think you can use `lapply` with a function that plots all the plots. Otherwise do it manually for each plot. – NelsonGon Jan 27 '19 at 16:05
  • 1
    @NelsonGon true...I guess the simple question is: How could I convert all text after "<-" to a string that we might use as a header, title, etc :) – Andrei Jan 27 '19 at 16:06
  • No need to save it as a variable. Just write it in `ggtitle("test_a")`. Otherwise x now takes on all data in test_a. – NelsonGon Jan 27 '19 at 16:07

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
  • 25,843
  • 5
  • 39
  • 58