Select Page

How to Remove Legend in ggplot2

You can remove a legend from a plot using the syntax legend.position=”none” for the plot theme. For example, library(ggplot2) ggplot(data = mtcars, aes(x = mpg, y = disp, color = vs)) + geom_point() + theme(legend.position=”none”) This tutorial...

How to Center Plot Title in ggplot2 with R

The easiest way to center a title using ggplot2 is to modify the plot.title component of the theme using element_text(hjust=0.5). For example, library(ggplot2) ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + ggtitle(“Automobile Weight vs Miles per...

How to Transpose a Data Frame in R

The easiest way to transpose a data frame is to use the transpose() function from the data.table library. For example, library(data.table) # get data data(“mtcars”) # transpose t_mtcars <- transpose(mtcars) # get row and colnames in order...

How to Add an Index column to a Data Frame in R

The easiest way to add a sequence of numbers as an index column is to use the nrow() function, for example, df$index <- 1:nrow(df) This tutorial will explain how to add a numeric ID column to a data frame with code examples. Table of contentsAdd Index Column to...