How to Use the plot() Function in R

plot() Function in R with Example

The plot() function in R is “used to create a plot”. It has many options and arguments to control many things, such as the plot type, labels, titles, and colors. For example, plot(x, y) creates a scatter plot of x and y numeric vectors. Syntax plot(x, y, type, main, xlab, ylab, pch, col, las, bty, … Read more

How to Use the Barplot() Function in R

Bar Chart in R: Bar Plot in R using barplot() Function

The barplot() function in R is “used to create a bar chart with vertical or horizontal bars”. The bars can have different colors, and their heights can be based on a vector or matrix of numeric values. Syntax barplot(height, xlab, ylab, main, names.arg, col) Parameters height: A vector or matrix containing numeric values used in … Read more

How to Use the chartr() Function in R

R chartr() Function - How to Substitute characters of String

The chartr() function in R is “used to replace an instance of an old character with a new character in the specified string“. It replaces all the matches of the existing characters of a string with the new characters specified as the argument. Syntax chartr(old, new, x) Parameters old: The old string is to be substituted. … Read more

How to Change Legend Title in R ggplot2

How to Change Legend Title in ggplot2

To change the legend title in R ggplot2 depending on the type of aesthetic mapping used in your plot, you can use the “labs()” or “scale_fill_discrete()” function. Method 1: Using the labs() function The easiest way to change a legend title in ggplot2 is to use the “labs()” function. The labs() function changes the axis, legend, … Read more

How to Use the stat_summary() Function in R

What is stat_summary() Function in R

The stat_summary() function in R “allows for tremendous flexibility in the specification of summary functions”. The stat_summary() function calculates various summary statistics for data points, such as the mean, median, maximum, minimum, or standard deviation. It takes a summary function as an argument, such as mean, median, max, min, sd, q1, or q3, to name … Read more

How to Save a Plot in R

To save a plot in R: Call a function to open a new graphics file, such as png(), jpg(), bmp(), or tiff(). In the next step, call the plot() function to generate the graphics image. At last, call the function to close the graphics file. If you don’t provide an external path, it will save … Read more

How to Center the Plot Title in R ggplot2

How to Center the Plot Title in R ggplot2

To center the plot title in R ggplot2, follow the below steps: Create a “ggplot” object. Use the “plot title” to explain the main findings. Use the “theme()” function and specify the argument “hjust = 0.5” in the “element_text()” function. Example: R Program to  Center the Plot Title in ggplot2 # Load required packages library(ggplot2) # … Read more

How to Use the scale_fill_brewer() Function in R

What is scale_fill_brewer() Function in R

The scale_fill_brewer() function in R is “used from a set of palettes in the RColorBrewer package”. It changes the fill color of ggplot2 plots, such as boxplots, barplots, violin plots, dot plots, etc. Syntax scale_fill_brewer( type = “seq”, palette = 1, direction = 1, aesthetics = “fill” ) Parameters type: It is one of “seq” … Read more

How to Use the scale_colour_distiller() Function in R

What is scale_colour_distiller() Function in R

The scale_colour_distiller() function in R is “used to apply a color scale to the color aesthetic of a plot”. The scale_colour_distiller() function uses a color scale generated using a distiller color scheme, a type of color scheme that emphasizes perceptual uniformity and is commonly used for data visualization. Syntax scale_colour_distiller( type = “seq”, palette = … Read more

How to Use the scale_fill_distiller() Function in R

What is scale_fill_distiller() Function in R

The scale_fill_distiller() function in R is “used to apply a color scale to the fill aesthetic of a plot”. For example, scale_fill_distiller(palette = “Spectral”, n = 7) will create a color scale with 7 colors from the Spectral palette. Syntax scale_fill_distiller( type = “seq”, palette = 1, direction = -1, values = NULL, space = “Lab”, … Read more