0

I'm using ggplot2 to graph two different groups of data. I have too many points and so when I label them they overlap. This caused me to need to use ggrepel with ggplot2. I need to make the size and color of these groups different, which I was able to achieve, but I'm unable to suppress segment lines from point to label for one group (one group doesn't have points altogether). This is probably easier to understand with my code:

labels=c("a1","a2","a3","a4","a5","b1","b2","b3","b4","b5")
x<-c(4.7,4.6,-11.0,6.2,11.4,6.1,6.1, -9.9,-9.7,-0.3)
y<-c(13.7,13.6,7.6,9.5,-5.6,10.6,10.6,5.8,9.0,0.1)
mydata<-data.frame(labels,x,y)
mydata$color <- "firebrick"
for(i in 1:5)mydata[i,4]="darkgray"
mydata$segment <- "firebrick"
for(i in 1:5)mydata[i,5]=NA
mydata$size <- 8
for(i in 1:5)mydata[i,6]=5

Which results in data frame:

> mydata
   labels     x    y color       segment size
1      a1   4.7 13.7  darkgray     NA       5
2      a2   4.6 13.6  darkgray     NA       5
3      a3 -11.0  7.6  darkgray     NA       5
4      a4   6.2  9.5  darkgray     NA       5
5      a5  11.4 -5.6  darkgray     NA       5
6      b1   6.1 10.6 firebrick firebrick    8
7      b2   6.1 10.6 firebrick firebrick    8
8      b3  -9.9  5.8 firebrick firebrick    8
9      b4  -9.7  9.0 firebrick firebrick    8
10     b5  -0.3  0.1 firebrick firebrick    8

Code below to graph:

ggplot(data=mydata, aes(x=x, y=y)) + geom_point(data=mydata[6:10,],color="firebrick") + 
  geom_text_repel(data=mydata,aes(label=labels),color=mydata$color,show.legend=FALSE,
              size=mydata$size,
              segment.color=mydata$segment) +
  geom_hline(yintercept=0, color="black", size=0.5)+geom_vline(xintercept=0,color="black",size=0.5)+
  theme(panel.background=element_rect(color="black",fill="white"),panel.grid.major.y=element_blank(),panel.grid.minor.y=element_blank(),
    panel.grid.major.x=element_blank(),panel.grid.minor.x=element_blank())

There may very well be an easier way to do this. I need lines for the red labels b1-b5, but not for a1-a5. resulting plot here

As always, any ideas are greatly appreciated! I'll update if I figure out a solution.

EDIT: what I need from this graph:

  • a1-a5 is smaller, gray, no points (just labels) and no lines connecting labels to points (These are all achieved with above code)
  • b1-b5 is larger, red, has points and labels and has lines connecting points to labels (I cannot seem to get segment lines connecting to points while simultaneously suppressing them for a1-a5)

Thanks, J

J. Sweet
  • 15
  • 8
  • Seeing your code, it seems that you do not want to draw dots for a1-a5? Is that correct? – jazzurro Sep 07 '16 at 22:48
  • it's not clear to me what your expected output is... – Cyrus Mohammadian Sep 08 '16 at 01:43
  • a1-a5 should be gray, font smaller, and not have segment lines (which are all achieved with this code). b1-b5 should be red, font larger. Both achieved with this code. The issue I need to fix is that I want lines connecting labels to points for b1-b5 and NOT a1-a5. Will update question to try and make more clear. The way I set it up to refer to color and size variables works for font color and size, but for some reason it isn't working for segment line color. – J. Sweet Sep 08 '16 at 15:57

0 Answers0