我正在使用geom_segment()
将y轴点映射到x轴,并使用facet_wrap
将数据分成两个图; 但是,y轴点出现在两个图中。
如何只显示每个facet_wrap
相关的必要y轴点?
示例代码
dat <- structure(list(temp = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3,
4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5), rev = c(-5,
-11, -20, -29, -40, -9, -20, -32, -45, -57, -12, -24, -37, -50,
-62, -7, -20, -36, -52, -67, -5, -13, -23, -35, -47, -12, -24,
-36, -48, -58), type = c("Type 1", "Type 1", "Type 1", "Type 1",
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1",
"Type 1", "Type 1", "Type 1", "Type 1", "Type 2", "Type 2", "Type 2",
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2",
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2"), model = c("A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C",
"C", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "C", "C",
"C", "C", "C")), .Names = c("temp", "rev", "type", "model"), row.names = c(NA,
-30L), class = "data.frame")
p1 <- ggplot(dat, aes(temp, rev, color = model)) +
geom_line() + geom_point() + geom_segment(aes(x = 0, xend = temp, yend = rev), linetype = "dashed", color = "grey") +
facet_wrap(~type, scales = "free") + scale_y_continuous(breaks = dat$rev)
p1
情节
ggsave("file.png", width = 6, height = 8)
。你也可以尝试使用ggrepel包来微调标签的位置。 - Brian