Found 2038 Articles for R Programming

How to display mean in a histogram using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:11:57

4K+ Views

To display mean in a histogram using ggplot2, we can use geom_vline function where we need to define the x-intercept value as the mean of the column for which we want to create the histogram. Also, we can change the size of the line for mean in the histogram by using size argument inside geom_vline function.Consider the below data frame −x

How to change the space between bars in a barplot in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:10:07

524 Views

By default, the space between bars is equal irrespective of the number of bars in the plot. If we want to have different space between bars then space arguments need to be used inside the barplot function but the first value does not make an impact because the first space is fixed between Y-axis and the first bar. For example, if we have a vector x that contains three values then the barplot with different space between bars can be created by using the below command −barplot(x,space=c(0.5,0.1,0.5))Example Live Demox

How to extract the residuals and predicted values from linear model in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:08:20

3K+ Views

The residuals are the difference between actual values and the predicted values and the predicted values are the values predicted for the actual values by the linear model. To extract the residuals and predicted values from linear model, we need to use resid and predict function with the model object.Consider the below data frame −Example Live Demox1

How to create a qqplot with confidence interval in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 11:02:59

2K+ Views

A qqplot is the plot of quantiles that helps to understand whether the supplied data comes from the specified distribution, mostly it is used to check whether the data follows normal distribution or not. If we want to create the qqplot with confidence interval then qqPlot function of car package can be used as shown in the below example.Consider the below data frame −Example Live Demox

How to concatenate numerical columns in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:59:30

712 Views

If we have values scattered in multiple columns in an R data frame then we need to combine them and create a single column, this combining process is called concatenation. The scatteredness of the values mostly happens when the data is not well formatted to be loaded in R. Therefore, to deal with this scatteredness problem we need to use apply function.Consider the below data frame −Example Live Demox1

How to convert a date to quarter and year in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:55:21

889 Views

Most of the times the date data is available only in date format and there is not information about the quarter of the year. The quarter of the year is required when we want compare the quarters or do some time series analysis. For the conversion of date into quarter and year can be done by using as.yearqtr function from zoo package as shown in the below examples.Examplelibrary(zoo) as.yearqtr("2021-01-19", format="%Y-%m-%d")Output[1] "2021 Q1"as.yearqtr("2021-04-19", format="%Y-%m-%d")[1] "2021 Q2"as.yearqtr(c("2021-04-19", "2020-05-24", "2020-11-09"), format="%Y-%m-%d")[1] "2021 Q2" "2020 Q2" "2020 Q4" Example Live Demox1Read More

How to print the vector elements vertically in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:53:34

709 Views

By default, the vector elements are printed horizontally in R environment, suppose a vector x has five values then they will be printed as 1, 2, 3, 4, 5. And if we want to print them vertically then the output will be −1 2 3 4 5The printing of these values in vertical form can be done by using the below command −cat(paste(x),sep="")Example Live Demox1

How to plot the regression line starting from origin using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:50:03

857 Views

The regression line starting from origin means that the intercept of the model is dropped from the regression model. To plot the regression line starting from origin, we can use the formula by subtracting 1 in geom_smooth function of ggplot2 package.Consider the below data frame −Example Live Demox

How to extract only factor columns name from an R data frame?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:45:54

1K+ Views

To extract only factor column names from an R data frame, we can use names function with Filter by selecting only factor columns using as.factor. For example, if we have a data frame called df that contains some factor columns then the extraction of names of these factor columns can be done by using names(Filter(is.factor,df)).Consider the below data frame −Example Live Demox1

How to display a line in segment of a plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 10:39:34

263 Views

To display a line in segment of a plot, we can use geom_segment function of ggplot2 package where we need to pass the initial and the ending values for both the axes. For example, if we have data frame called df that contains x and y then a scatterplot with a line segment can be created by using the below command −ggplot(df,aes(x,y))+geom_point()+ geom_segment(aes(x=xstart,xend=xlast,y=ystart,yend=ylast))Consider the below data frame −Example Live Demox

Advertisements