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.
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.
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
Under this method, the color of the line plot is controlled using the scale_color_manual() function, which is passed with a vector of colors for each line.
Similarly, linetype can be controlled using scale_linetype_manual() function, this also takes a vector as an input but the values passed are different linetypes for each line.
Example: Controlling line color and type manually