How to Change Legend Size in ggplot2 (With Examples)

How to Change Legend Size in ggplot2 (With Examples)

To change the legend size of ggplot2 in R, you can use the “theme()” function, where you can control the text size, key size, and other aspects of the legend’s appearance. Syntax ggplot(data, aes(x=x, y=y)) + theme(legend.key.size = unit(1, ‘cm’), #change legend key size legend.key.height = unit(1, ‘cm’), #change legend key height legend.key.width = unit(1, … Read more

How to Change Legend Labels in ggplot2 (With Examples)

How to Change Legend Labels in ggplot2 (With Examples)

The scale_fill_discrete() function is “used to change the legend labels for discrete fill aesthetics in ggplot2.” You can use the labels argument to specify the new labels. Syntax p + scale_fill_discrete(labels=c(‘label1’, ‘label2’, ‘label3’, …)) Example 1: Using scale_fill_discrete() function # Load the ggplot2 library library(ggplot2) # Create a sample data frame df <- data.frame( category … Read more

How to Remove a Legend in ggplot2 (With Examples)

How to Remove a Legend in ggplot2 (With Examples)

To remove a legend from ggplot2 in R, use the “theme()” function along with the legend.position argument set to “none” to completely remove the legend from the plot. Example 1: Set legend.position = none Here’s an example that demonstrates how to create a scatter plot without a legend: library(ggplot2) # Create a sample data frame df … Read more

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, follow these steps. 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 dev.off() function to close the graphics file. If you don’t provide an external path, it … 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