如何使用coord_polar对齐标签?

时间:2015-02-26 10:39:22

标签: r heatmap

我正在尝试制作一个圆形"热图"在R中,找到了coord_polar的解决方案,以及如何在图上分发标签。

我的问题是情节周围的标签似乎居中,长名称与情节重叠。我无法使用hjustvjust将文字与图表的边缘对齐。

我的代码和我的数据的一部分:

library(reshape)
library(ggplot2)

data <- data.frame(id=c("S_subsp_houtenae_str_ATCC_BAA-1581","S_Heidelberg_S_1_7","S_Haifa_S_11_3","S_Infantis_S_2_3","S_Newport_S_1_4","S_Bredeney_S_1_3","S_Saint_Paul_S_1_5","S_Bovismorbificans_S_3_8","S_Saintpaul_str_SARA26","S_London_S_6_7","S_Mbandaka_S_7_5","S_Corvallis_S_5_6","S_San_Diego_S_9_5","S_Javiana_str_10721"),
            A.C2=c(0,0,0,0,0,0,0,0,0,0,0,2,0,0),Col156=c(0,0,0,0,0,4,0,0,0,0,0,0,0,0),
            ColRNAI=c(0,8,0,0,8,8,8,0,8,0,0,0,0,0),FIB=c(0,0,0,0,10,0,0,10,10,0,0,0,0,0),
            FII=c(0,0,0,0,0,0,0,12,12,0,0,0,0,0),HI2=c(0,15,0,0,15,15,0,0,0,0,0,0,0,0),
            HI2A=c(0,15,0,0,15,15,0,0,0,0,0,0,0,0),I1=c(0,17,17,17,0,0,0,0,0,0,0,17,17,0),
            I2=c(0,0,0,0,0,0,0,0,0,0,0,18,18,18),N=c(0,0,0,0,0,0,0,19,19,19,19,0,0,0),
            P=c(20,20,20,20,20,20,20,0,0,0,0,0,0,0),Q1=c(0,22,0,0,22,0,0,0,0,0,0,22,0,0))
data <- transform(data,id=factor(id,levels=unique(id)))

data.m <- melt(data) 
data.m$var2 = as.numeric(data.m$variable) + 15
y_labels = levels(data.m$variable)
y_breaks = seq_along(y_labels) + 15

sequence_length = length(unique(data.m$id))
first_sequence = c(1:(sequence_length%/%2)) 
second_sequence = c((sequence_length%/%2+1):sequence_length) 
first_angles =c(90 - 180/length(first_sequence) * first_sequence)
second_angles = c(-90 - 180/length(second_sequence) * second_sequence)

Palette <- c("#f1f1f1","#302013","#614126","#58DB41","#638A5C","#62D585","#579134","#B8DD95","#9ED84D","#4B6FC8","#2A344D","#47689B","#315CEE","#D9AB68","#E09B33","#FE9E2A","#D97B0C","#6A2F45","#A02A77","#E1C73E","#D16F60","#C13420","#DA435C","#E20338","#000000","#999999")

p = ggplot(data.m, aes(x=id, y=var2, fill=factor(value))) + 
geom_tile(colour="white") + 
scale_fill_manual(values=Palette) +
scale_y_discrete(breaks=y_breaks, labels=y_labels) + 
theme(panel.background=element_blank(),
axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.x=element_text(angle= c(first_angles,second_angles),size=8),
axis.ticks=element_blank(),
axis.text.y=element_blank(),
legend.position="none")

p = p + coord_polar()
plot(p) 

2 个答案:

答案 0 :(得分:1)

我在coord_polar()中遇到类似的问题,标签没有对hjust=vjust=做出回应,因此没有像我一样对齐。

此处显示的解决方案https://stackoverflow.com/a/28846989/4340137,是使用geom_text()手动标记数据。

提供链接的示例可以满足您的所有需求。不幸的是,我无法通过更复杂的数据结构快速完成工作,所以不要让我留下这个评论。

其他人可能可以编辑以包含确切的代码。

答案 1 :(得分:0)

在RStudio中,当我运行以下内容并进行缩放时,除了最长的标签之外,所有标签都在圆圈之外,这可能意味着顶部的绘图边距太紧(或者您可能会考虑缩短名称或使用\ n换新线)。我将axis.text.y参数更改为theme。我也无法让左上角的奇怪传说消失。即便如此,插入的图也会遇到您所描述的重叠问题。

ggplot(data.m, aes(x=id, y=var2, fill=factor(value))) + 
  geom_tile(colour="white") + 
  scale_fill_manual(values=Palette) +
  scale_y_discrete(breaks=y_breaks, labels=y_labels) + 
  theme(panel.background=element_blank(), axis.title=element_blank(), panel.grid=element_blank(),
        axis.text.x=element_text(angle= c(first_angles,second_angles),size=8, vjust=-1), # vjust=-1
        axis.ticks=element_blank(), legend.position="none", 
        axis.text.y=element_text(vjust = -2), legend.position="none") +
  coord_polar()

enter image description here