Found 2038 Articles for R Programming

Fill bars in a base R barplot with colors based on frequency.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 05:00:31

375 Views

Suppose we have a vector that contains only frequencies and we want to create a bar chart in base R using these frequencies with color of bars based on frequencies, therefore, we can use barplot function and providing the color of the bars with as shown in the below ExamplesThe function is as follows −heat.colors functionExample 1To fill the bars in a base R barplot with colours based on frequency, use the command given below −Frequency_1

How to create a circle filled with a color in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 12:07:41

666 Views

We can create a circle in R by using draw.circle function of plotrix package and default color of the circle is white. If we want to change the color of a circle then we can use col argument and pass the desired colors.For Example, if we want to create a blue colored circle then we can use the command given below −draw.circle(5, 5, 2, col="blue")Check out the below Example to understand how it works.ExampleTo create a circle filled with a colour, use the command given below −plot(1:10, type="n") OutputIf you execute the above command, it generates the following Output −To ... Read More

How to change a text value in an R data frame?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:57:50

3K+ Views

To change a text value in an R data frame, we can simply use replace function.For Example, if we have a data frame called df that contains a column say Names and one of the names say Raj is misspelled as Raaj then we can replace Raaj with Raj by using the command given below −df$Names

How to find mathematical set using a data frame column in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:45:44

62 Views

A mathematical set is a collection of unique elements or a collection of elements that are different from each other. If we want to find a mathematical set using a data frame column then we can simply use unique function.For Example, if we have a data frame called df that contains a column say X then we can find the mathematical set using X with the help of below command −unique(df$X)Example 1Following snippet creates a sample data frame −x

How to change the color of gridlines of a ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:32:22

3K+ Views

To change the color of gridlines of a ggplot2 graph in R, we can use theme function with panel.grid.major and panel.grid.minor arguments where we can set the minor and major gridlines color of the plot panel to desired color.To understand how it can be done, check out the below Example.ExampleFollowing snippet creates a sample data frame −x

How to change the plot border color of a ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:25:46

4K+ Views

To change the plot border color of a ggplot2 graph in R, we can use theme function with panel.background argument where we can set the border of the plot panel using element_rect to desired color.To understand how it can be done, check out the below Example.ExampleFollowing snippet creates a sample data frame −x

Find the sum of a column values based on another numerical column in R.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:16:35

291 Views

To find the sum of a column values based on another numerical column in R, we can use with function and define the sum by subsetting the column with the help of single square brackets.For Example, if we have a data frame called df that contains two columns say X and Y then we can find the sum of values in X when Y is greater than 10 by using the command with the following −(df,sum(X[Y10]))Example 1Following snippet creates a sample data frame −x1

How to create horizontal line in xyplot in R?

Nizamuddin Siddiqui
Updated on 02-Nov-2021 11:03:24

531 Views

To create horizontal line in xyplot, we can use abline function.For Example, if we have a data frame called df that contains two columns say X and Y and we want to create a scatterplot between X and Y using xyplot with a horizontal line at Y = 2 then we can use the command given below −xyplot(Y~X,df,abline=c(h=2))ExampleFollowing snippet creates a sample data frame −x

R Programming to find the column name for row maximum in a data.table object.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:49:43

340 Views

If we have a data.table object and we want to find the column name for row maximum then we can use max.col function.For Example, if we have a data.table object called DT then we can find the column name for row maximum by using the below command −DT[,names(.SD)[max.col(.SD,ties.method="first")]]Example 1Following snippet creates a sample data frame −x1

Remove rows from a data frame that exists in another data frame in R.

Nizamuddin Siddiqui
Updated on 02-Nov-2021 10:34:33

5K+ Views

To remove rows from a data frame that exists in another data frame, we can use subsetting with single square brackets. This removal will help us to find the unique rows in the data frame based on the column of another data frame.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −x

Advertisements