我正在尝试使用ggplot
为我在图表中创建的细分添加标签dat <- data.frame(start <- c(-1.05113647, -0.63911585, -0.62791554),
end <- c(0.37491159, -0.13911585, -0.12791554),
order <- c("Sei whale", "Probeagle", "Northern fur seal"),
pos <- c(1, 2, 3))
ggplot(dat) +
geom_segment(aes(x=start, y=start, xend=end, yend=start), colour = "blue", size = 2) +
scale_y_reverse() +
xlab("PC1")+
ylab(" ")+
theme_linedraw() +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
theme(aspect.ratio = 0.3) +
theme(legend.position="none") +
theme(axis.ticks = element_blank(), axis.text.y = element_blank())
我想知道如何将“订单”中的名称添加到各自的细分中。
答案 0 :(得分:1)
您可以使用geom_text()
:
library(ggplot2)
ggplot(dat) +
geom_segment(aes(start, start, xend = end, yend = start),
colour = "blue",
size=4) +
geom_text(aes((end + start) / 2,
y = start,
label = order),
color = 'grey75',
size = 3,
nudge_y = .005) +
scale_y_reverse() +
xlab("PC1")+
ylab(" ")+
theme_linedraw() +
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
aspect.ratio = 0.3,
legend.position="none",
axis.ticks = element_blank(),
axis.text.y = element_blank())
结果并不是特别漂亮,我增加了segment
size
,让文字可见。
答案 1 :(得分:1)
您可以尝试geom_dl()
包中的directlabels
并稍微展开x轴:
library(ggplot2)
library(directlabels)
ggplot(dat) +
geom_segment(aes(x = start, y = start, xend = end, yend = start),
colour = "blue", size = 2) +
scale_y_reverse() +
geom_dl(aes(end, start, label = order),
method = list(dl.trans(x = x + 0.2), "last.bumpup", cex = 0.60)) +
scale_x_continuous(expand = c(0, 0.2)) +
labs(x = "PC1", y = " ") +
theme_linedraw() +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank()) +
theme(aspect.ratio = 0.7) +
theme(legend.position = "none") +
theme(axis.ticks = element_blank(), axis.text.y = element_blank())
给出了:
注意:我将aspect.ratio
更改为0.7以使其更具可读性。