R: How to Label Specific Contours using direct.label
contour, ggplot2, r
Solution
I couldn't stand to see an old question unanswered with such an easy fix.
The simple problem was the mapping inside `stat_contour()`. Your call should be:
v<-ggplot(volcano, aes(x=X1,y=X2,z=value)) # specify the mapping properly
e<-v + stat_contour(aes(colour=..level..), breaks=c(160, 170, 180))
direct.label(e)
With the breaks not included in the aes mapping, and the `colour=..level..` included.
Problem
I'm relatively new to `ggplot2`, and I'm having trouble adding appropriate labels to my contours. Using the classic volcano example, I can add labels to the default contour plot: ``` library(plyr) library(ggplot2) library(directlabels) library(reshape) volcano<-melt(volcano) v<-ggplot(volcano, aes(x,y,z=z)) e<-v + stat_contour(aes(colour=..level..)) direct.label(e) ``` In the above example, the labels are added appropriately, but things become more complicated if I try to specify my own break points for the contours: ``` e<-v + stat_contour(aes(breaks=c(160, 170, 180), colour=..level..)) direct.label(e) ``` Now, the contours are specified by the breaks I have provided, but labels still appear for all of the default contours. How do I only plot only labels for the graphed contours? A related issue, how would I plot labels for contour levels not included in the default? Say a break of 165: ``` e<-v + stat_contour(aes(breaks=c(165), colour=..level..)) direct.label(e) ``` Thanks for any help!