-1

I've tried many combinations of geom_text() but couldn't get the exact output I need.

I want the labels to remain at same y-axis limit unless they intersect If they do intersect, I need them to offset in the y-direction so that they will not intersect. I want them efficient so that not much space is left between them.

geom_text_repel() is not a fix because it throws some error. can someone explain why there is an error when I use geom_text_repel()

This is my Data and code:

library(ggplot2)
library(ggrepel)
Data=data.frame(Group=c("Group 2","Group 1","Group 3","Group 2","Group 5","Group 4","Group 6",
                        "Group 7","Group 4","Group 3","Group 1","Group 5","Group 6","Group 7",
                        "Group 2","Group 4","Group 6","Group 7","Group 3","Group 1"),
                Fruit=c("apple","apple","apple","mango","apple","apple","apple","apple","mango","mango",
                        "mango","mango","mango","mango","orange","orange","orange","orange","orange",
                        "orange"),
                Percentage=c(68.46846847,77.35849057,72.72727273,26.12612613,76.31578947,62.79069767,
                             71.05263158,69.23076923,30.23255814,25,20.75471698,23.68421053,23.68421053,
                             23.07692308,5.405405405,6.976744186,5.263157895,7.692307692,2.272727273,
                             1.886792453))
ggplot(Data,aes(x=reorder(Fruit,Percentage),y=Percentage,color=Group,label=paste0(round(Percentage),"%")))+geom_point(size=4)+coord_flip()+scale_y_continuous(limits=c(0,100))+
  theme(panel.grid.major.y=element_line(color="gray90",size = 0.7),panel.background=element_blank(),strip.background=element_blank(),panel.border=element_rect(color="black",fill=NA,size=1))+
  labs(x="",y="% of people",color="")+geom_text_repel(aes(label=paste0(round(Percentage),"%")))

The error is:

Error in .Call("_ggrepel_repel_boxes", PACKAGE = "ggrepel", data_points,  : 
  Incorrect number of arguments (11), expecting 9 for '_ggrepel_repel_boxes'

This is my output: Current Output

This is what I want: Required Output

Vedha Viyash
  • 708
  • 1
  • 8
  • 19
  • 3
    Use `ggrepel::geom_text_repel()` instead of `geom_text(position=position_jitter())`. – pogibas May 21 '18 at 10:28
  • Possible duplicate of [How to reduce geom\_text overlap](https://stackoverflow.com/questions/42949875/how-to-reduce-geom-text-overlap) – pogibas May 21 '18 at 10:31
  • tried that, It is not giving the desired result, I need them exactly in the same line along the y axis but offset when they intersect, they should offset the minimum so the labels are barely touching and offset only in the positive y axis. – Vedha Viyash May 21 '18 at 10:32
  • 2
    You can specify argument `direction = "x"` ("direction in which to adjust position of labels"). – pogibas May 21 '18 at 10:41
  • still the same error – Vedha Viyash May 21 '18 at 10:42

1 Answers1

2

You would have to play with the arguments of geom_text_repel, but maybe this would be a starting point:

library(ggrepel)
library(ggplot2)
ggplot(Data,aes(x=reorder(Fruit,Percentage),y=Percentage,
                color=Group,
                label=paste0(round(Percentage),"%")))+
  geom_point(size=4)+
  coord_flip()+scale_y_continuous(limits=c(0,100))+
  theme(panel.grid.major.y=element_line(color="gray90",size = 0.7),
        panel.background=element_blank(),
        strip.background=element_blank(),
        panel.border=element_rect(color="black",fill=NA,size=1))+
  labs(x="",y="% of people",color="")+
  ggrepel::geom_text_repel(direction = "y", 
                           segment.colour = "transparent",
                           nudge_x = 0.2, point.padding = 0.1, box.padding = 0.1)

enter image description here

SeGa
  • 9,454
  • 3
  • 31
  • 70