Found 2038 Articles for R Programming

How to display text in base R plot with outline?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:05:39

333 Views

The display of text in base R plot with outline is not possible, for this purpose we would need to use shadowtext function of TeachingDemos package. The shadowtext function will be applied after creating the plot in base R.We will have to provide the location of the text inside the plot and some other arguments such as text that needs to be displayed, color of outline, and size for better display.Example 1Use the following code to display text in base R plot with outline −plot(1) shadowtext(1.2, 1.2, "Point at 1", col="white", cex=2)OutputIf you execute the above given code, it generates ... Read More

Differentiate between categorical and numerical independent variables in R.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:02:54

665 Views

For categorical variable, each level is considered as an independent variable and is recognized by factor function. On the other hand, the numerical independent variable is either continuous or discrete in nature.Check out the Example given below for linear regression model summary to understand the difference between categorical and numerical independent variables.ExampleFollowing snippet creates a sample data frame −x

How to create an upper triangular matrix using vector elements in R?

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

2K+ Views

To create an upper triangular matrix using vector elements, we can first create the matrix with appropriate number of columns and rows then take the transpose of that matrix. After that we will assign the lower triangular matrix elements to 0.The selection of number of rows and columns plays an important role here so we need to be careful while choosing them.Check out the examples given below to understand how it can be done.Example 1Following snippet creates a vector −x1

Set values in categorical column to numeric values in R data frame.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:55:36

3K+ Views

To set values in categorical column to numeric values in R data frame, we can use combine function c.For Example, if we have a data frame called df that contains a categorical column say C which has two categories as Low and High and if we want to represent these categories with 1 and 10 then we can use the below command −df$C

How to change legend for multiple histograms using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:48:50

1K+ Views

If we create histogram for multiple categories using ggplot2 then the legend is generated automatically based on the categories. And if we want to change that legend or create a histogram with different legend values having different colors for histograms then scale_fill_manual function can be used as shown in the below example.ExampleFollowing snippet creates a sample data frame −Height

How to add a caption in a ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:52:30

600 Views

To add a caption in a ggplot2 graph in R, we can use labs function. For Example, if we have a data frame called df that contains two columns say X and Y and we want to create scatterplot between X and Y with a caption as a Note that says “Linear Relation Display” then we can use the below command −ggplot(df,aes(X,Y))+geom_point()+labs(caption="Note: Linear Relation Display")ExampleFollowing snippet creates a sample data frame −x

How to find the location of installed packages in R in windows operating system?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:45:27

3K+ Views

To find the location of installed packages in R in windows operating system, we can use the command mentioned below −.libPaths()This will return the location of installed packages in the first line and the program files in the second line.Use the below mentioned code to find the location of installed packages in windows operating system −.libPaths() OutputIf you execute the given snippet, it generates the following Output −[1] "C:/Users/Nizam/Documents/R/win-library/4.0" [2] "C:/Program Files/R/R-4.0.4/library"Use the below mentioned code to find the location of installed packages in windows operating system −installed.packages()[1:20, ] OutputIf you execute all the above given snippets as a single ... Read More

How to make all values in an R data frame unique?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:41:54

347 Views

To make all values in an R data frame unique, we can use make.unique function but firstly we would need to unlist the data frame values and read them with as.character. For Example, if we have a data frame called df that contains duplicate as well as nonduplicate values then we can make all the values unique by using the below command −df[]

How to remove question mark from corrplot in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:35:41

970 Views

When we have NAs present in the data frame or matrix then correlation matrix contains NA values. Now if we create the correlation matrix plot using corrplot function the Output display question marks.If we want to create the correlation matrix without question marks then we can use the na.label argument and set it to blank as shown in the below Example.ExampleFollowing snippet creates a sample matrix −M

How to find the row corresponding to a nearest value in an R data frame?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 07:40:32

3K+ Views

To find the row corresponding to a nearest value in an R data frame, we can use which.min function after getting the absolute difference between the value and the column along with single square brackets for subsetting the row.To understand how it works, check out the examples given below.Example 1Following snippet creates a sample data frame −ID

Advertisements