Label of data overlapping

Asked

Viewed 55 times

1

I want to keep data labels organised in a way that does not overlap with each other.

The idea is to keep them above geom_point or below.. depending on the position of the line.

How can I proceed ?

Follow the simplified command and a figure as an example of the problem.

dt <- data.frame(periodo = c("JUN", "JUL","AGO","SET","OUT","NOV","DEZ"), 
                 diam = c(2.76,2.75,2.55,2.81,2.1,2.2,2.34,2.89,2.96,2.77,2.88,2.99,2.10,2.11))
dt$Variedade <- rep(c("A", "B"), each = nrow(dt)/2)
dt$periodo <- factor(dt$periodo, levels = c("JUN", "JUL","AGO","SET","OUT","NOV","DEZ"))

ggplot(dt, aes(x = periodo, y = diam, group = Variedade)) + 
  geom_point() +
  geom_line() +
  geom_label(aes(label = diam)) +
  scale_y_continuous(breaks = c(2,4), limits = c(1,5))

exemplo

2 answers

3


You can use the option nudge_* to move the labels. For greater control, as lines cross, you can make the shift depending on the position of one Variety relative to another:

adj <- with(dt, diam[Variedade == "A"] - diam[Variedade == "B"])
adj <- ifelse(adj < 0, -.2, .2)

ggplot(dt, aes(x = periodo, y = diam, group = Variedade, color = Variedade)) +
  geom_point() +
  geom_line() +
  geom_label(aes(label = diam), nudge_y = c(adj, -adj)) +
  scale_y_continuous(limits = c(1,4))

inserir a descrição da imagem aqui

2

Use the package ggrepel and the function geom_text_repel

library(ggrepel)
dt <- data.frame(periodo = c("JUN", "JUL","AGO","SET","OUT","NOV","DEZ"), 
             diam = c(2.76,2.75,2.55,2.81,2.1,2.2,2.34,2.89,2.96,2.77,2.88,2.99,2.10,2.11))
dt$Variedade <- rep(c("A", "B"), each = nrow(dt)/2)
dt$periodo <- factor(dt$periodo, levels = c("JUN", "JUL","AGO","SET","OUT","NOV","DEZ"))
ggplot(dt, aes(x = periodo, y = diam, group = Variedade, label = diam)) + 
  geom_point() +
  geom_line()+
  ggrepel::geom_label_repel(fill = "white") +
  scale_y_continuous(breaks = c(2,4), limits = c(1,5))
  • When running the above command with the argument ggrepel::geom_label_repel(fill = "white") is returning me to the following error: Error in repel_boxes2(data_points = as.matrix(x$data[, c("x", "y")]), : pacote 'Rcpp_precious_remove' não oferece a função 'Rcpp'.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.