Search code examples
rplotaxis-labels

Problems with the size of the labels of the plots in R


I'm having problems with the size of the labels of the plots. So, this is the code:

column1 <- c(0.18936045, 0.55010315,  0.23474801, 0.02578839)
column2 <- c(0.20522653, 0.51235168, 0.26060781, 0.02181398 )

example_data <- 
  data.frame(
    rowNames = c('[-2.34898,-0.83219]', '(-0.83219,0.684599]', '(0.684599,2.20139]', '(2.20139,3.71818]'),
    column1 = column1,
    column2 = column2
  )

plot(data = example_data, column1 ~ column2, xlab = "Marginal probs scaledsci",
     ylab = "Actual probs from data", pch = 20, col = 'blue')
text(data = example_data, column1 ~ column2, labels = rowNames, cex = .6, pos = 2.99, col = 'red')

This is the plot I obtain: plot obtained

So, I would like to have all the points with their labels visible. So, could someone help me with this?


Solution

  • For plots I prefer ggplot. When I run into issues with the positions of labels (overlaps with datapoints and such) I often add the functionalities of the package ggrepel. Example below.

    library(ggplot2)
    library(ggrepel)
    ggplot(example_data, aes(x = column1, y = column2)) + geom_point(size = 4, col = "blue") + labs(x = "Marginal probs scaledsci", y = "Actual probs from data") + geom_text_repel(label = example_data$rowNames, col = "red", nudge_y = 0.02) +
    theme_bw() +  theme(text = element_text(size = 15))
    

    The nudge_y parameter moves your label with 0.02 units of your y-variable. Its optional but looked better in my opinion.

    Plot