0

I am using ggplot2 to create a dot plot. My data is basically in the form of three columns x_axis, y_axis and z_axis, x_axis and y_axis together represents a pair and z_axis represents the pair count.

So I am plotting x_axis vs y_axis and using z_axis to color the dots. There are certain situations where I would like to skip plotting a particular count, for eg: The count of 1 occurs multiple times and sometimes I would like to skip plotting 1, but the legend should show 1. The following is my code:

    > new<-read.table("PB1_combo.txt", header=T, sep="\t")
    > bp <-ggplot(data=new, aes(x_axis,y_axis, colour=factor(z_axis)), size=z_axis) +                                 
    geom_point(size=5)
    > bp + ggtitle("PB1-PB1")
    > last_plot()+ scale_colour_discrete(name="Counts")
    > last_plot()+ theme_bw()


  Sample data from PB1_combo.txt
  x_axis  y_axis  z_axis
    14      576     2
    394     652     2
    759     762     2
    473     762     2
    65      763     3
    114     390     2
    762     763     4
    758     762     2
    388     616     2
    217     750     2
    65      762     2
    473     763     2
    743     759     2
    65      213     2
    743     762     2
Mdhale
  • 815
  • 2
  • 15
  • 22
  • Please give us sample data that illustrates your problem. We don't have `PB1_combo.txt`. The best way to do this is to either simulate something and post the code or post `dput(head(new))`. Both methods are described [here](http://stackoverflow.com/q/5963269/903061). – Gregor Thomas Aug 16 '13 at 18:44
  • If you're not clear on how to write a question with data, read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – SlowLearner Aug 16 '13 at 19:14

1 Answers1

1

First, you should create a factor z_axis. That way, even if not all possible values are present, R will be aware of them.

new$Count <- factor(new$z_axis)

(You should really choose a name other than new by the way.)

Then you can just subset your data however and display the missing levels in the legend by using drop=FALSE in the call to scale_color_discrete:

ggplot(data=new[new$Count!="2", ], aes(x_axis,y_axis, colour=Count), size=z_axis) +                                 
  geom_point(size=5) +
  ggtitle("PB1-PB1") +
  scale_colour_discrete(name="Counts", drop=FALSE) +
  theme_bw()

enter image description here

See this question, actually.

Community
  • 1
  • 1
Peyton
  • 7,266
  • 2
  • 29
  • 29