Jacky Lau
Jacky Lau

Reputation: 31

R ggplot gives multiple color at the margin of text

I'm making some dot plots with ggplot2 in R. Here is the overall result.

overall When zoomed in, I can see multiple colors at the margin of the labels, as well as the dots, which makes it difficult to distinguish different groups. text dot

Here are part of the code.

png(file=paste("dot/",tumorType,"_",cellLine,"_1.png",sep=""), bg="transparent",width = 2048, height = 2048, units = "px",res = 300)

p = ggplot()
p = p + geom_point(data=rocData,aes(x=NES_up,y=NES_down,shape=type,color=CGC),size=3)
p = p + geom_text(data=rocData,aes(x=NES_up+0.005,y=NES_down,label = labels),size = 2,hjust = 0,colour="black")

p = p + scale_shape_manual(values=c(95,124,21))# + scale_color_brewer(palette="Set1")
p = p + scale_color_manual(values=c("grey30","red"),labels = c("normal", "CGC"))
p = p + scale_size_manual(values=c(12,8,8))
p = p + theme(plot.title = element_text(size=16,face = "bold",vjust=1.5))
p = p + theme(legend.title = element_text(size = 10),legend.key.size = unit(6,"mm"))
p = p + theme(plot.background=element_rect(fill="white", colour=NA))
p = p + labs(colour="gene type",shape="data group")
p1 = p + coord_cartesian(ylim = c(miny,maxy),xlim = c(minx,maxx))
p1 = p1 + labs(y="ES(down)")

p1 = p1 + theme(legend.position="none",axis.ticks.x=element_blank(),axis.text.x=element_blank(),axis.title.x=element_blank(),plot.margin=unit(c(12,0,0,6),"mm"))
p1= p1 + theme(axis.title.y=element_text(size=10,hjust=0.5,vjust=0.5))

p2 = p + coord_cartesian(ylim = c(miny,maxy),xlim = c(-0.02,0.02))
p2 =p2 + theme(axis.title=element_blank(),axis.text=element_blank(),axis.ticks=element_blank(),plot.margin=unit(c(12,6,0,0),"mm"))
p2 = p2 + scale_x_continuous(breaks=c(0))

p3 = p + coord_cartesian(ylim = c(-0.02,0.02),xlim = c(minx,maxx))
p3 = p3 + labs(x="ES(up)",y="")  #shape = 19/21
p3 = p3 + theme(legend.position="none",plot.margin=unit(c(0,0,6,6),"mm")) + scale_y_continuous(breaks=c(0),labels=c("0.00"))
p3= p3 + theme(axis.title.x=element_text(size=10,hjust=0.5,vjust=0.5))
p3= p3 + theme(axis.title.y=element_text(size=10,hjust=0.5,vjust=0.5))

p4 = p + coord_cartesian(ylim = c(-0.02,0.02),xlim = c(-0.02,0.02))
p4 = p4 + theme(legend.position="none")
p4 = p4 + theme(axis.title=element_blank(),axis.ticks=element_blank(),axis.text=element_blank(),panel.background=element_blank(),panel.grid=element_blank(),plot.margin=unit(c(0,6,6,0),"mm"))

grid.arrange(p1,p2,p3,p4, ncol=2, nrow=2,widths=c(4,2), heights=c(4,1),main=textGrob(paste(tumorType,"_",cellLine,sep=""), gp=gpar(fontface="bold",fontsize=16), hjust=0.6,vjust=2.5))

dev.off()

The entire plot is composed of four parts with gridExtra package. So my questions is how can I remove the unnecessary margin color in the plot?

Upvotes: 1

Views: 321

Answers (2)

Mehdi Nellen
Mehdi Nellen

Reputation: 9004

This behavior is dependent on your OS. png takes an argument called type which you can try to change to one of these options:

type = c("cairo", "Xlib", "quartz")  

In my case Xlib and quartz got rid of it. You can check what your default is using getOption("bitmapType")

My recommendation is to use vector images:

  • svg
  • ps
  • pdf

You can import them into almost any software (latex, web, libre office)

Upvotes: 1

csgillespie
csgillespie

Reputation: 60492

Two options:

  1. Save as a vector format, e.g. ps or pdf
  2. Increase the resolution, of the png, so something like:

    dpi = 1000
    png("plot.png", width=6*dpi, height=6*dpi, res=dpi)
    ## plotting commands
    dev.off()
    

Upvotes: 2

Related Questions