How to use Default Parameters In R Language

In this method, inbuilt attributes are passed to the function with appropriate values to generate the requirement. Thus, in order to change the color, col or color attribute has to be passed with the column name from the dataframe on the basis of which distinction has to be made.

Syntax: aes(col= column_name)

Similarly, to control line type, linetype attribute needs to be passed with the column name of the dataframe.

Syntax: aes(linetype= column_name )

Example: Controlling line color and type using default parameters

R




library("ggplot2")
  
function1 <- function(x){x**2}
function2 <- function(x){x**3}
function3 <- function(x){x/2}
function4 <- function(x){2*(x**3)+(x**2)-(x/2)}
  
df = data.frame(x = -2:2,
              values = c(function1(-2:2),
                       function2(-2:2),
                       function3(-2:2),
                       function4(-2:2)),
              fun = rep(c("function1","function2",
                        "function3","function4"))
)
  
ggplot(df, aes(x, values, color = fun, 
               linetype = fun)) + geom_line()


Output:

Control Line Color and Type in ggplot2 Plot Legend in R

In this article, we will see how to control line color and type in ggplot2 plot legend in the R programming language.

Similar Reads

Using Default Parameters

In this method, inbuilt attributes are passed to the function with appropriate values to generate the requirement. Thus, in order to change the color, col or color attribute has to be passed with the column name from the dataframe on the basis of which distinction has to be made....

Changing manually

...