本文介绍了ggplot2圆形热图,看起来像一个甜甜圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图用ggplot2创建循环热图,这样我就可以在圆周上使用更多数量的标签。我想让它看起来更像是一个中间有空洞的甜甜圈,但同时不会丢失任何行(它们需要被压缩)。有任何想法吗?下面是我的代码。 library(reshape) library(ggplot2) nba< - read.csv(http://datasets.flowingdata.com/ppg2008.csv) nba $名称< - with(nba,reorder(Name,PTS)) nba .m nba.m p = ggplot(nba.m,aes(Name,variable))+ geom_tile(aes(fill = value),color =white)+ scale_fill_gradient(low =white,high =steelblue) p 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) $ (1)将因子转换为数字并添加一个偏移量,(2)手动指定y-limits和(3) )手动设置y轴中断和标签: library(reshape) library(ggplot2) #使用ggplot2 0.9.2.1 nba< - read.csv(http://datasets.flowingdata.com/ppg2008.csv) nba $ Name< - with( nba,reorder(Name,PTS)) nba.m nba.m #将因子水平转换为数字+量化以确定洞的大小。 nba.m $ var2 = as.numeric(nba.m $变量)+ 15 #需要使用scale_y_discrete添加标签和分隔符。 y_labels = levels(nba.m $ variable) y_breaks = seq_along(y_labels)+ 15 p2 = ggplot(nba.m,aes(x = Name,y = (0,var2,fill = value))+ geom_tile(color =white)+ scale_fill_gradient(low =white,high =steelblue)+ ylim 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) I'm trying to create circular heatmap with ggplot2 so I can use a larger number of labelsaround 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). Any ideas? Code for what I have is below. Thanks!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) 解决方案 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.1nba <- 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) + 15p2 = 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) 这篇关于ggplot2圆形热图,看起来像一个甜甜圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 00:00