2

I want to plot data from function. For example:

Make data and load libraries:

# Load ggplot2
library(ggplot2)

# Create Data
data <- data.frame(
  group=LETTERS[1:5],
  value=c(13,7,9,21,2)
)

This plotting works as intended:

# Basic piechart
ggplot(data, aes(x="", y=value, fill=group)) +
  geom_bar(stat="identity", width=1, color="white") +
  coord_polar("y", start=0) +

  theme_void() # remove background, grid, numeric labels

But if I try to plot from inside of function:

a <- function(data)
{
  # Basic piechart
  ggplot(data, aes(x="", y=value, fill=group)) +
    geom_bar(stat="identity", width=1, color="white") +
    coord_polar("y", start=0) +

    theme_void() # remove background, grid, numeric labels
  return()
}


a(data)

it just gives me output:

NULL

without drawing any plot.

Question: How to make draw plot from function in my example?

example taken from: https://www.r-graph-gallery.com/piechart-ggplot2.html

vasili111
  • 6,032
  • 10
  • 50
  • 80

1 Answers1

2

The options to make your function work.

Option - I

Remove the line return (), which basically returns NULL when the function finishes (your plot is local to the function a, and the plot is not accessible outside the function, and no parameter is passed to return).

a <- function(data)
{
  # Basic piechart
  ggplot(data, aes(x="", y=value, fill=group)) +
    geom_bar(stat="identity", width=1, color="white") +
    coord_polar("y", start=0) +
    theme_void() # remove background, grid, numeric labels
}

Option-II

Save your plot on a local variable to the function and return it up on completion.

a <- function(data)
{
  # Basic piechart
  p <- ggplot(data, aes(x="", y=value, fill=group)) +
    geom_bar(stat="identity", width=1, color="white") +
    coord_polar("y", start=0) +
   theme_void() # remove background, grid, numeric labels
  return(p)
}

Option - III

Use environment = environment() which sets the environment variable explicitly to the current environment when calling ggplot. You can read more about at Use of ggplot() within another function in R.

a <- function(data)
{
  # Basic piechart
  p <- ggplot(data, aes(x="", y=value, fill=group), environment = environment()) 
    p + geom_bar(stat="identity", width=1, color="white") +
    coord_polar("y", start=0) +  
    
    theme_void() # remove background, grid, numeric labels
  
}

Now you can use a(data) to print the output.

Output

ego_function

Community
  • 1
  • 1
deepseefan
  • 3,701
  • 3
  • 18
  • 31
  • So, does the `environment = environment()` makes visible all global variables for `ggplot`? If no, what it does? If yes, why it is necessary and is there any other way that does not expose all global variables to `ggplot` that is called inside function? – vasili111 Oct 21 '19 at 04:55
  • Have a look [Use of ggplot() within another function in R](https://stackoverflow.com/questions/5106782/use-of-ggplot-within-another-function-in-r) for alternatives and discussions. – deepseefan Oct 21 '19 at 04:57
  • Am I understand correctly that `environment = environment()` makes visible for `ggplot` all global variables? – vasili111 Oct 21 '19 at 05:06
  • Thank you. Updates where very useful. – vasili111 Oct 21 '19 at 13:27