How to Assign Colors to Categorical Variable in ggplot2 Plot in R ? Last Updated : 24 Jun, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to assign colors to categorical Variables in the ggplot2 plot in R Programming language. Note: Here we are using a scatter plot, the same can be applied to any other graph. Dataset in use: YearPointsUsers1201130user12201220user23201315user34201435user45201550user5 To Create an R plot, we use ggplot() function and to make it scatter plot we add geom_point() function to ggplot() function. By Default, the plot has following colors. Example: R # Load Library library(ggplot2) # Create DataFrame for Plotting. data <- data.frame(Year = c(2011, 2012, 2013, 2014, 2015), Points = c(30, 20, 15, 35, 50), Users = c("user1", "user2", "user3", "user4", "user5")) # Create ggplot2 ScatterPlot. ggplot(data, aes(Year, Points, color = Users)) + geom_point(size = 10) Output: ScatterPlot using ggplot2 with default colors In R Programming, we have many in built function for creating our own discrete scales such as scale_fill_manual, scale_size_manual, scale_shape_manual, scale_linetype_manual, etc. To assign desired colors to categorical data, we use one of them scale_color_manual() function, which is used to scale (map) the manual colors. Syntax : scale_color_manual(values) Parameter : values : A set of aesthetic values to map the data. Here we take desired set of colors. Return : Scale the manual values of colors on data. Example: R # Load Library library(ggplot2) # Create DataFrame for Plotting data <- data.frame(Year = c(2011, 2012, 2013, 2014, 2015), Points = c(30, 20, 15, 35, 50), Users = c("user1", "user2", "user3", "user4", "user5")) # Create a ScatterPlot with fixed colors of # points(data). ggplot(data, aes(Year, Points, color = Users)) + geom_point(size = 10)+ scale_color_manual(values = c("green", "orange", "red", "yellow", "blue")) Output : Plot with fixed colors Comment More infoAdvertise with us Next Article How to Assign Colors to Categorical Variable in ggplot2 Plot in R ? E erkrutikpatel Follow Improve Article Tags : R Language R-ggplot Similar Reads How to change Colors in ggplot2 Line Plot in R ? A line graph is a chart that is used to display information in the form of series of data points. It utilizes points and lines to represent change over time. Line graphs are drawn by plotting different points on their X coordinates and Y coordinates, then by joining them together through a line from 2 min read How to Use a Variable to Specify Column Name in ggplot in R When working with ggplot2 in R, you might find yourself in situations where you want to specify column names dynamically, using variables instead of hard-coding them. This can be particularly useful when writing functions or handling data frames where the column names are not known in advance. This 4 min read How Do I Map Categorical Variables to Color the Outline of Points in a 3D Scatter Plot in R Plotly? 3D scatter plots allow visualization of three variables in a spatial context. When adding a fourth dimension, such as a categorical variable, we often map this variable to color. However, in certain cases, you may want to color the outline (borders) of the points based on a categorical variable to e 4 min read Change Theme Color in ggplot2 Plot in R A theme in ggplot2 is a collection of settings that control the non-data elements of the plot. These settings include things like background colors, grid lines, axis labels, and text sizes. we can use various theme-related functions to customize the appearance of your plots, including changing theme 4 min read How To Change facet_wrap() Box Color in ggplot2 in R? In this article, we will discuss how to change facet_wrap() box color in ggplot2 in R Programming language. Facet plots, where one subsets the data based on a categorical variable and makes a series of similar plots with the same scale. Facetting helps us to show the relationship between more than t 3 min read Like