Reputation: 727
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?
Upvotes: 3
Views: 4104
Reputation: 2598
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.
Upvotes: 5
Reputation: 9836
First thing I made sure I had the lastest version of directlabels.
And then:
dat <- reshape2::melt(volcano)
brks <- c(100, 120, 140, 160)
g <- ggplot(dat, aes(x = Var1, y = Var2, z = value)) +
geom_contour(aes(colour = ..level..), breaks = brks)
g
direct.label(g, list("bottom.pieces", colour='black'))
Upvotes: 0