Press "Enter" to skip to content

How to add colors to your plot?

Zigya Acadmey 0

In this article, you’ll learn how to add/change the color of points, text and lines. We will also be creating a plot and will try various aesthetic manipulations in the same plot, like changing the text color, line color and point color.

In general, we use these arguments inside the plot() function itself.

We can define the color in the plot() function using col. In the example below, we will see how to add the colors to a line plot. We can change the type of the plot, but the color syntax will be exactly the same, just add a col to the main plot() function.

Usage

> plot(x, y, main="My plot", col="blue", col.main="red", col.lab="green")

Arguments

xthe coordinates of points in the plot. Alternatively, a single plotting structure, function or any R object with a plot method can be provided.
ythe y coordinates of points in the plot, optional if x is an appropriate structure.
mainan overall title for the plot. The default is "XY plot".
colcolor for the plotting “character”. The default is "black".
col.mainSets or changes the color for the title.
col.labThis attribute sets or changes the color for x and y labels.

Adding color to marker

We can add colors to a marker in a plot using the col argument inside the plot() function. We can simply assign the col argument a color.

Here is the plot with a red marker color.

> y <- c(10:100)
> x <- y*y
> plot(x, y, main="Curve", col="red") 
Plot with red marker

Adding color to title

Similarly, we can add a color for the title of our plot with the col.main argument in the plot() function. We can simply assign the col.main argument a color.

Here is the plot with a blue title color.

> plot(x,y,main="Curve",col.main="blue")
plot with blue title

Adding color to x and y label

Similarly, we can add a color for the x and y label of our plot with the col.lab argument in the plot() function. We can simply assign the col.lab argument a color.

Here is the plot with orange x and y label.

> plot(x,y,main="Curve",col.lab="orange")

Conclusion

In this article we saw, how colors can be added to the plots and how we can add colors for the x and y axes as well.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published.