Reputation: 885
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
Upvotes: 0
Views: 105
Reputation: 7396
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()
See this question, actually.
Upvotes: 1