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