Moritz Schmid
Moritz Schmid

Reputation: 395

Overplotting of labels, data points and more (w geom_dl & direct label)

I can not find a solution for my overplotting problem. If somebody could help me find a solution I would appreciate that a lot.

My data look like this (csv format): http://pastebin.com/embed_js.php?i=Cnfpkjsz

This is the code I am running:

     library(dplyr)
    library(gdata)
    library(ggplot2)
    library(directlabels)


    all<-read.xls('all_auto_bio_adjusted.xls')

    all$station<-as.factor(all$station)
    all$automatic<-log(all$automatic)
    all$averagebiol<-log(all$averagebiol)
    all$stdevbiol<-log(all$stdevbiol)

    pd <- position_dodge(.9) 

    allp<-ggplot(data=all, aes(y=averagebiol, x=automatic, colour=group)) +
      geom_errorbar(aes(ymin=averagebiol-stdevbiol, ymax=averagebiol+stdevbiol), colour="red", width=.1, position=pd) +
      geom_point(aes(size=size), show_guide = TRUE) +
      geom_abline(intercept=0, slope=1) +
      stat_smooth(method="loess",se=FALSE,colour='blue') +
      geom_dl(aes(label=shortname),method="last.bumpup",cex = 1.3, hjust = 1) +
      facet_wrap(~station,nrow=2)+
      xlab("auto")  +
      ylab("manual") +
      ggtitle("Comparison of automatic vs manual identification") +
      scale_y_continuous(limits=c(0, max(all$averagebiol + all$stdevbiol))) +
      theme_bw() +
      theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=15,angle=0,hjust=1,vjust=0,face="bold"),  axis.title.x = element_text(colour="grey20",size=20,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=20,angle=90,hjust=.5,vjust=1,face="bold"),legend.position="right")

allp

I tried around quite a bit with different geom_dl methods but can't find the right one. Is there one that can plot above the error bars?

If there is no good one for me. What could I do to at least have the labels plotted nicely so that I can rearrange them myself in photoshop?

Producing this plot

Thanks a lot for your input!

Upvotes: 2

Views: 1454

Answers (3)

Moritz Schmid
Moritz Schmid

Reputation: 395

Indeed I ended up faceting by station and group after input from Harrop.

Here is my code (The underlying data changed slightly)

library(dplyr)
library(gdata)
library(ggplot2)
library(directlabels)


all<-read.xls('all_auto_bio_adjusted_c.xls')
all$size.new<-sqrt(all$size.new)
all$station<-as.factor(all$station)
all$group.new<-factor(all$group, levels=c('C. hyperboreus','C. glacialis','Special Calanus','M. longa','Pseudocalanus sp.','Copepoda'))

pd <- position_dodge(w = 50) 

allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group.new, group=group.new)) + 
  geom_abline(intercept = 0, slope = 1) +
  geom_point(aes(size = size.new), show_guide=TRUE, position=pd) +
  scale_size_identity()+
  geom_errorbar(aes(ymin = averagebiol - stdevbiol, ymax = averagebiol +   stdevbiol),colour = "grey", width = 0.1, position=pd) +
  facet_grid(group.new~station, scales="free") +
  xlab("Automatic identification") + ylab("Manual identification") +
  ggtitle("Comparison of automatic vs manual identification") + 
  theme_bw() + 
  theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=15,angle=0,hjust=1,vjust=0,face="bold"),  axis.title.x = element_text(colour="grey20",size=20,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=20,angle=90,hjust=.5,vjust=1,face="bold"), legend.position="none", strip.text.x = element_text(size = 12, face="bold", colour = "black", angle = 0), strip.text.y = element_text(size = 12, face="bold", colour = "black"))

allp

Upvotes: 0

mucio
mucio

Reputation: 7119

enter image description here

I hope you wanted somehting like this.

My code:

allp<-ggplot(data=all, aes(y=averagebiol, x=automatic, colour=group)) +
  geom_point(aes(size=size), show_guide = TRUE) +
  geom_abline(intercept=0, slope=1) +
  stat_smooth(method="loess",se=FALSE,colour='blue') +
  facet_wrap(~station,nrow=2)+
  xlab("auto")  +
  ylab("manual") +
  ggtitle("Comparison of automatic vs manual identification") +
  scale_y_continuous(limits=c(0, max(all$averagebiol + all$stdevbiol + 1, na.rm=T))) +
  theme_bw() +
  theme(plot.title = element_text(lineheight=.8, face="bold", size=20,vjust=1), axis.text.x = element_text(colour="grey20",size=15,angle=0,hjust=.5,vjust=.5,face="bold"), axis.text.y = element_text(colour="grey20",size=15,angle=0,hjust=1,vjust=0,face="bold"),  axis.title.x = element_text(colour="grey20",size=20,angle=0,hjust=.5,vjust=0,face="bold"), axis.title.y = element_text(colour="grey20",size=20,angle=90,hjust=.5,vjust=1,face="bold"),legend.position="right")+
  geom_errorbar(aes(ymin=averagebiol-stdevbiol, ymax=averagebiol+stdevbiol), colour="red", width=.1, position=pd) +
  geom_text(aes(label = shortname, y = averagebiol+stdevbiol), vjust = -.3) 

I removed your line of code for the labels and I introduced this one:

  geom_text(aes(label = shortname, y = averagebiol+stdevbiol), vjust = -.3) 

This is just putting the labels on top of the Error bar, with a small adjustment.

Also I modified this part:

scale_y_continuous(limits=c(0, max(all$averagebiol + all$stdevbiol + 1, na.rm=T)))

With the labels on top, some of the labels where covered by the gray bar, so I increased a bit the value of the X bar (+1), but the max didn't want to work correctly with my little addition, so I had to remove the NA values.

Upvotes: 1

Tom Harrop
Tom Harrop

Reputation: 678

I'm not familiar with directlabels but if you want to move the labels to the top you could just do it with geom_text():

allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group)) + 
  geom_errorbar(aes(ymin = averagebiol - stdevbiol, ymax = averagebiol + stdevbiol),
                colour = "red", width = 0.1, position = pd) +
  geom_point(aes(size = size), show_guide = TRUE) +
  geom_abline(intercept = 0, slope = 1) +
  stat_smooth(method = "loess", se = FALSE, colour = "blue") +
  facet_wrap(~station, nrow = 2) +
  xlab("auto") + ylab("manual") +
  ggtitle("Comparison of automatic vs manual identification") + 
  scale_y_continuous(limits = c(0, max(all$averagebiol + all$stdevbiol)))

allp + geom_text(aes(label = shortname, y = averagebiol + stdevbiol), vjust = -0.1) 

However still seems too busy to distinguish between the groups. How about skipping the text labels and facetting over station and group? Here's a possible start, if you like it you would need to tune it...

allp <- ggplot(data = all, aes(y = averagebiol, x = automatic, colour = group)) + 
  geom_errorbar(aes(ymin = averagebiol - stdevbiol, ymax = averagebiol + stdevbiol),
                colour = "grey", width = 0.5, position = pd) +
  geom_point(aes(size = size), show_guide = TRUE) +
  geom_abline(intercept = 0, slope = 1) +
  stat_smooth(method = "loess", se = FALSE, colour = "blue") +
  facet_grid(station ~ group) +
  xlab("auto") + ylab("manual") +
  ggtitle("Comparison of automatic vs manual identification") + 
  scale_y_continuous(limits = c(0, max(all$averagebiol + all$stdevbiol))) +
  theme_minimal()

allp

Upvotes: 1

Related Questions