I'm trying to make a contour plot with specified breaks and labels at those breaks. I tried to add labels at the breaks using either direct.label or geom_dl, but failed.
dat <- melt(volcano)
brks <- c(100, 120, 140, 160)
g <- ggplot(dat, aes(x = Var1, y = Var2, z = value)) +
geom_contour(colour = 'black', breaks = brks)
g
That part works fine, but when I try to add the labels:
direct.label(g, list("bottom.pieces", colour='black'))
I receive the error: Need colour or fill aesthetic to infer default direct labels.
And, when I try:
g + geom_dl(aes(label = brks), method = 'bottom.pieces')
I get: Error: Aesthetics must be either length 1 or the same as the data (5307): label, x, y, z
Any suggestions?
I think that I have come to a workaround to show the labels using geom_dl:
library(lattice)
library(directlabels)
dat <- melt(volcano)
brks <- c(100, 120, 140, 160)
g <- ggplot(dat, aes(x = Var1, y = Var2, z = value)) +
geom_contour(colour='black', breaks = brks)+
geom_dl(aes(label=..level..), method="bottom.pieces",
stat="contour",breaks = brks)
g
Just indicate in geom_dl that you want to label the levels (aes(label=..levels..)) contained in the breaks (breaks=brks), so it knows the labels to be shown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With