Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to use my function f() regarding ggplot, but it is not working. Please tell me how to solve this.

f= function(){

   geom_point() + geom_line() 

}

data_frame = tibble(y_axis = c(1,2,3,1,1,1), 

                    x_axis = c("AD", "B", "C", "A", "B", "C"),

                    g = c("buy", "sell", "buy", "sell", "buy", "sell"))

g2 <- ggplot(data_frame, aes(x=x_axis, y=y_axis, colour = g,group =  g)) + f()

g2

1 Answer

0 votes
by (107k points)

Just add the ggplot objects with the function, but you have to make sure that it must be in the form of a list. So this is what you need to perform:

f <- function() {

    list(geom_point(), geom_line())

}

Just add that to any ggplot object as you have intimated in your code:

ggplot(df, aes(x,y)) + f()

If you are interested to know what is R programming language then do check out the blog. 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...