Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

r - Circular Heatmap that looks like a donut

I'm trying to create circular heatmap with ggplot2 so I can use a larger number of labels around the circumference of a circle. I'd like to have it look more like a donut with an empty hole in the middle but at the same time not losing any rows (they would need to be compressed).

Code for what I have is below.

library(reshape)
library(ggplot2)

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))


p = ggplot(nba.m, aes(Name,variable)) + geom_tile(aes(fill = value), colour = "white") +           scale_fill_gradient(low = "white", high = "steelblue") 
p<-p+opts(
panel.background=theme_blank(),
axis.title.x=theme_blank(),
axis.title.y=theme_blank(),
panel.grid.major=theme_blank(),
panel.grid.minor=theme_blank(),  
axis.text.x=theme_blank(),
axis.ticks=theme_blank()
)


p = p + coord_polar() 
plot(p) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a solution accomplished by (1) converting factor to numeric and adding an offset, (2) manually specifying y-limits and (3) manually setting y-axis breaks and labels:

library(reshape)
library(ggplot2)
# Using ggplot2 0.9.2.1

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 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

p2 = ggplot(nba.m, aes(x=Name, y=var2, fill=value)) +
     geom_tile(colour="white") +
     scale_fill_gradient(low = "white", high = "steelblue") +
     ylim(c(0, max(nba.m$var2) + 0.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))


ggsave(filename="plot_2.png", plot=p2, height=7, width=7)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...