I want to add another legend that tells me what ring of a circular heat map represents (from outer ring to inner ring).
I tried the following from another answer previously:
library(reshape)
library(ggplot2)
library(plyr)
nba <- read.csv("http://datasets.flowingdata.com/ppg2008.csv")
nba$Name <- with(nba, reorder(Name, PTS))
nba.m <- melt(nba)
nba.m <- ddply(nba.m, .(variable), transform, value = scale(value))
# Convert the factor levels (variables) to numeric + quanity to determine size of hole.
nba.m$var2 = as.numeric(nba.m$variable) + 15
# Labels and breaks need to be added with scale_y_discrete.
y_labels = levels(nba.m$variable)
y_breaks = seq_along(y_labels) + 15
nba.labs <- subset(nba.m, variable==levels(nba.m$variable) [nlevels(nba.m$variable)])
nba.labs <- nba.labs[order(nba.labs$Name),]
nba.labs$ang <- seq(from=(360/nrow(nba.labs))/1.5, to=(1.5* (360/nrow(nba.labs)))-360, length.out=nrow(nba.labs))+80
nba.labs$hjust <- 0
nba.labs$hjust[which(nba.labs$ang < -90)] <- 1
nba.labs$ang[which(nba.labs$ang < -90)] <- (180+nba.labs$ang)[which(nba.labs$ang < -90)]
p2 = ggplot(nba.m, aes(x=Name, y=var2, fill=value)) +
geom_tile(colour="white") +
geom_text(data=nba.labs, aes(x=Name, y=var2+1.5,
label=Name, angle=ang, hjust=hjust), size=3) +
scale_fill_gradient(low = "white", high = "steelblue") +
ylim(c(0, max(nba.m$var2) + 1.5)) +
scale_y_discrete(breaks=y_breaks, labels=y_labels) +
coord_polar(theta="x") +
theme(panel.background=element_blank(),
axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.x=element_blank(),
axis.ticks=element_blank(),
axis.text.y=element_text(size=5))
print(p2)
However, instead of getting the legend, I'm having this error message instead:
Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.
Any solutions?
Thanks in advance!
It's not entirely clear to me what you're looking for but this may be it. You were originally using
scale_y_discrete(breaks=y_breaks, labels=y_labels)
to project to a continuous variable,var2
, inaes(x=Name, y=var2, fill=value)
. By changing that toscale_y_continuous(breaks=y_breaks, labels=y_labels)
you can get the categorical labels listed fornba.m$variable
.UPDATE
I'm not sure what you're trying to do here -those values are not blank in the center because there's data there, removing
scale_y_continuous(breaks=y_breaks, labels=y_labels)
limits the scale of the y-axis such that the date is no longer graphed. That's why you're not seeing the middle filled when that line of code is removed. At any rate, if that's what you're looking for, what you need to do is deletescale_y_continuous(breaks=y_breaks, labels=y_labels)
and turn off the labels for the y-axis, then manually add those labels usinggrob
. I'm sure there's a better way to accomplish what you need but this will get you started at least.