Home > database >  label X axis on ggscatter
label X axis on ggscatter

Time:07-12

I have a dataframe with 2 columns to do a correlation. The data is of integer number from 1 to 10. However, when I use the ggscatter, the axis (X and Y) uses a scale of 0, 2.5, 5, 7.5 and 10. I want to change it. It doesn't need to be all the number, however it never will be 2.5 or 7.5.

How can I make this?

Here are my code (with a sample to make this example).

X_AXIS = sample(c(1:10),size = 100, replace = TRUE)
Y_AXIS = sample(c(1:10),size = 100, replace = TRUE)

novaTabela = data.frame(X_AXIS, Y_AXIS)
ggscatter(novaTabela, x = "X_AXIS", y = "Y_AXIS", 
          add = "reg.line", 
          cor.coef = TRUE, cor.method = "pearson")

CodePudding user response:

just by adding the labels and breaks to the scale using scale_x_continuous (or scale_y_continuous) as we do in ggplot. Also, note that, whatever change you make to the label, define those for breaks argument too.

library(ggpubr)

X_AXIS = sample(c(1:10),size = 100, replace = TRUE)
Y_AXIS = sample(c(1:10),size = 100, replace = TRUE)

novaTabela = data.frame(X_AXIS, Y_AXIS)

p <- ggscatter(novaTabela, x = "X_AXIS", y = "Y_AXIS", 
          add = "reg.line", 
          cor.coef = TRUE, cor.method = "pearson")

p   scale_x_continuous(labels = seq(1, 10, 3), breaks = seq(1, 10, 3))

ggpubr plot with manually defined scale label

  • Related