Add italics r with correlation coefficient to the scatter plot chart in ggplot

I am trying to use the code below to create a simple scatter plot with a correlation coefficient that would be italicized r in the plot.

data(mtcars) # Load required libraries require(ggplot2) # To derive the graphs require(ggthemes) # To apply ggplot themes to the chart require(scales) # For pretty breaks # Function to generate correlation coefficient for the charts corr_eqn <- function(x,y, digits = 2) { corr_coef <- round(cor(x, y), digits = digits) corr_coef <- expression(paste(italic(r)," = ", corr_coef)) return(corr_coef) } # Provide a scatter plot for income and health deprivation ggplot(mtcars, aes(x = drat, y = wt)) + geom_point(shape = 19, size = 2, aes(colour = as.factor(cyl))) + geom_smooth(colour = "red", fill = "lightgreen", method = 'lm') + ggtitle("Example") + xlab("drat") + ylab("wt") + scale_colour_tableau("tableau10") + geom_text(x = 3, y = 3, label = corr_eqn(mtcars$drat, mtcars$wt), parse = TRUE) + theme(legend.key = element_blank(), legend.background = element_rect(colour = 'black'), legend.position = "bottom", legend.title = element_blank(), plot.title = element_text(lineheight = .8, face = "bold", vjust = 1), axis.text.x = element_text(size = 11, vjust = 0.5, hjust = 1, colour = 'black'), axis.text.y = element_text(size = 11, colour = 'black'), axis.title = element_text(size = 10, face = 'bold'), axis.line = element_line(colour = "black"), plot.background = element_rect(colour = 'black', size = 1), panel.background = element_blank()) 

Does the code stop with a label ? in the console. Running the code using the lines:

 # geom_text(x = 3, y = 3, # label = corr_eqn(mtcars$drat, mtcars$wt), parse = TRUE) + 

commented, generates the following diagram: scatter plot

I assume that my function to generate an equation of the format r = 0.7 does not work, how can I fix it?

+9
r expression annotations ggplot2 scatter-plot
source share
2 answers

As you suspected, you just need to customize your function. You could use substitute as shown in this answer , but you can also just use paste here.

 corr_eqn <- function(x,y, digits = 2) { corr_coef <- round(cor(x, y), digits = digits) paste("italic(r) == ", corr_coef) } 

enter image description here

Note that if you added as.character to what the original function returned, everything would work out. However, the result would be with corr_coef as a string instead of the actual correlation coefficient you wanted.

I should also add that geom_text may result in poor resolution if you do not put the labels and coordinates in the new data.frame.

 labels = data.frame(x = 3, y = 3, label = corr_eqn(mtcars$drat, mtcars$wt)) 

Then use the data argument and aes for geom_text :

 geom_text(data = labels, aes(x = x, y = y, label = label), parse = TRUE) 

See annotate with geom = "text" as another parameter that avoids the new data.frame.

+6
source share

Use the corrlY package

corrlY is a data visualization package for all types of correlation diagrams using the Plotly package.

 install.packages("devtools") devtools::install_github("maheshKulkarniX/corrlY") spearman<- corr_coef_spearman(variable1= cars$speed, variable2=cars$dist, decimal = 2) corr_scatterly(data=cars,x=cars$speed,y=cars$dist,corr_coef=spearman,xname="speed",yname="dist") 

For more information, see this link Data visualization using corrlY

Github repository data visualization package For all types of correlation diagrams using the Plotly package.

-2
source share

All Articles