在ggplot2中的轴上标注颜色轴或绘制矩形 [英] Colour axis labels or draw rectangles over axis in ggplot2
问题描述
我想通过使用ggplot2创建一个漂亮的树状图。
这是我正在做的一个可重复的例子:
$ library(ggplot2)
library(ggdendro)
data(mtcars)
x< - as.matrix(scale(mtcars) )
dd.row< - as.dendrogram(hclust(dist(t(x))))
mtcars_dendrogram< - ggdendrogram(dd.row,rotate = TRUE,theme_dendro = FALSE)+
labs(x =,y =Distance)+
ggtitle(Mtcars Dendrogram)+
theme(panel.border = element_rect(color =black ,fill = NA,size = .5),
axis.text.x = element_text(color =black,size = 10),
axis.text.y = element_text(color =black ,size = 10),
legend.key = element_rect(fill =white,color =white),
legend.position =bottom,legend.direction =horizontal,
legend.title = element_blank(),
panel.grid.major = element_line(color =#d3d3d3),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
plot.title = element_text(size = 14,family =Tahoma,face =bold),$ b $ (text = element_text(family =Tahoma))
mtcars_dendrogram <-mtcars_dendrogram +
annotate(rect,xmin = 0.6,xmax = 5.4,ymin = 0,ymax = 6.4,fill = rect,xmin = 5.6,xmax = 7.4,ymin = 0,ymax = 6.4,fill =blue,color =red,color =red,alpha = 0.1)+
注解蓝色,alpha = 0.1)+
注解(rect,xmin = 7.6,xmax = 11.4,ymin = 0,ymax = 6.4,fill =orange,color =orange,alpha = 0.1) +
geom_hline(yintercept = 6.4,color =blue,size = 1,linetype =dotted)
mtcars_dendrogram
这是结果
我想扩展矩形以覆盖xa红双喜。例如,如果我改变了,例如,
annotate(rect,xmin = 5.6,xmax = 7.4,ymin = 0,ymax = 6.4,fill =blue,color =blue,alpha = 0.1)
/ p>
annotate(rect,xmin = 5.6,xmax = 7.4,ymin = -1,ymax = 6.4,fill =blue ,color =blue,alpha = 0.1)
然后我得到这个
这就是我想要得到的结果(这个结果是用Photoshop改变的)
非常欢迎任何帮助。
您可以伪造左轴:
< pre $
mtcars_dendrogram <-mtcars_dendrogram +
annotate(rect,xmin = 0.6,xmax = 5.4,ymin = -1,ymax = 6.4,fill =red color =red,alpha = 0.1)+
注解(rect,xmin = 5.6,xmax = 7.4,ymin = -1,ymax = 6.4,fill =blue,color =blue, alpha = 0.1)+
annotate(rect,xmin = 7.6,xmax = 11.4,ymin = -1,ymax = 6.4,fill =orange,color =orange,alpha = 0.1)+
geom_hline(yintercept = 6.4,color =blue,size = 1,linetype =dotted)+
theme(axis.text.y = element_blank(),
axis.line。 y = element_blank(),
axis.ticks.y = element_blank())+
geom_text(aes(y = 0,x = 1:11,
label = c(carb ,wt,hp,cyl,disp,qsec,vs,mpg,drat,am,gear)),
hjust =
$ b
I want to create a beautiful dendrogram by using ggplot2.
This is a reproducible example of what I'm doing:
library(ggplot2)
library(ggdendro)
data(mtcars)
x <- as.matrix(scale(mtcars))
dd.row <- as.dendrogram(hclust(dist(t(x))))
mtcars_dendrogram <- ggdendrogram(dd.row, rotate = TRUE, theme_dendro = FALSE) +
labs(x="", y="Distance") +
ggtitle("Mtcars Dendrogram") +
theme(panel.border = element_rect(colour = "black", fill=NA, size=.5),
axis.text.x=element_text(colour="black", size = 10),
axis.text.y=element_text(colour="black", size = 10),
legend.key=element_rect(fill="white", colour="white"),
legend.position="bottom", legend.direction="horizontal",
legend.title = element_blank(),
panel.grid.major = element_line(colour = "#d3d3d3"),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
plot.title = element_text(size = 14, family = "Tahoma", face = "bold"),
text=element_text(family="Tahoma"))
mtcars_dendrogram <- mtcars_dendrogram +
annotate("rect", xmin = 0.6, xmax = 5.4, ymin = 0, ymax = 6.4, fill="red", colour="red", alpha=0.1) +
annotate("rect", xmin = 5.6, xmax = 7.4, ymin = 0, ymax = 6.4, fill="blue", colour="blue", alpha=0.1) +
annotate("rect", xmin = 7.6, xmax = 11.4, ymin = 0, ymax = 6.4, fill="orange", colour="orange", alpha=0.1) +
geom_hline(yintercept = 6.4, color = "blue", size=1, linetype = "dotted")
mtcars_dendrogram
This is the result
I want to extend the rectangles so that it covers the x-axis. If I change, for example,
annotate("rect", xmin = 5.6, xmax = 7.4, ymin = 0, ymax = 6.4, fill="blue", colour="blue", alpha=0.1)
to
annotate("rect", xmin = 5.6, xmax = 7.4, ymin = -1, ymax = 6.4, fill="blue", colour="blue", alpha=0.1)
Then I get this
This is what I want to obtain (this result was altered with Photoshop)
Any help is highly welcome. Thanks a lot beforehand.
解决方案 You can fake the left axis:
mtcars_dendrogram <- mtcars_dendrogram +
annotate("rect", xmin = 0.6, xmax = 5.4, ymin = -1, ymax = 6.4, fill="red", colour="red", alpha=0.1) +
annotate("rect", xmin = 5.6, xmax = 7.4, ymin = -1, ymax = 6.4, fill="blue", colour="blue", alpha=0.1) +
annotate("rect", xmin = 7.6, xmax = 11.4, ymin = -1, ymax = 6.4, fill="orange", colour="orange", alpha=0.1) +
geom_hline(yintercept = 6.4, color = "blue", size=1, linetype = "dotted") +
theme(axis.text.y = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_blank()) +
geom_text(aes(y = 0, x = 1:11,
label = c("carb", "wt", "hp", "cyl", "disp", "qsec", "vs", "mpg", "drat", "am", "gear")),
hjust = "right",
nudge_y = -.1))
这篇关于在ggplot2中的轴上标注颜色轴或绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!