我需要帮助将geom_segment连接到绘图外的geom_point。我尝试查找类似问题的答案,但是并不能解决我的问题。
我有一个带有两个y轴的图,如下所示:
我需要在(15,120)处添加一个橙色点。如您所见,每个橙色点都与一个橙色geom_segment连接。但是,geom_segment不能在图外绘制。我尝试添加coord_cartesian(ylim = c(0, 120), xlim = c(0,25), clip="off")
,但没有帮助。也许与双Y轴有关?
下面是我的脚本,下面可以找到My data
。
j <- ggplot() + theme_classic() +
geom_bar(mapping = aes(x = w$ki67pro[w$recurrence==1]), stat = "count", alpha=0.2, colour="#51BFC4", fill="#51BFC4")
yaks <- c(0,1,2,3,4,5,6,7,8,9)
j <- j %+% scale_y_continuous(expand=c(0,0), name = "XX", breaks=yaks, sec.axis = sec_axis(~ . * 1 , name = "YY", breaks=yaks, labels=c("0.000","0.025","0.050","0.075","0.100","0.125","0.150", "0.175","0.200", "0.225")), limits = c(0, 8.5))
j +
geom_point(mapping = aes(x = 0, y = 2.008368), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 1, y = 0.0000000), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 2, y = 1.0706320), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 3, y = 0.8202982), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 4, y = 1.1170213), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 5, y = 2.1384815), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 6, y = 0.0000000), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 7, y = 3.6090226), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 8, y = 6.6666667), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 10, y = 3.0868167), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 12, y = 1.5604681), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 15, y = 120.0000000), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 20, y = 8.0672269), size=2.5, shape=16, colour="#F0746B") +
geom_point(mapping = aes(x = 25, y = 0.0000000), size=2.5, shape=16, colour="#F0746B") +
geom_segment(mapping = aes(x = 0, y = 2.008368, xend = 1, yend = 0), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 1, y = 0, xend = 2, yend = 1.0706320), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 2, y = 1.0706320, xend = 3, yend = 0.8202982), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 3, y = 0.8202982, xend = 4, yend = 1.1170213), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 4, y = 1.1170213, xend = 5, yend = 2.1384815), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 5, y = 2.1384815, xend = 6, yend = 0.0000000), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 6, y = 0, xend = 7, yend = 3.6090226), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 7, y = 3.6090226, xend = 8, yend = 6.6666667), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 8, y = 6.6666667, xend = 10, yend = 3.0868167), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 10, y = 3.0868167, xend = 12, yend = 1.5604681), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 12, y = 1.5604681, xend = 15, yend = 120), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 15, y = 120, xend = 20, yend = 8.0672269), colour="#F0746B", alpha=1) +
geom_segment(mapping = aes(x = 20, y = 8.0672269, xend = 25, yend = 0), colour="#F0746B", alpha=1)
My data
w <- structure(list(recurrence = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L,
0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 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L, 2L, 1L, 6L, 1L, 4L, 12L, 3L, 4L, 3L, 2L, 6L,
5L, 4L, 3L, 4L, 4L, 4L, 3L, 5L, 4L, 5L, 4L, 1L, 3L, 3L, 4L, 0L,
3L)), .Names = c("recurrence", "ki67pro"), class = "data.frame", row.names = c(1L,
2L, 3L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 18L, 19L, 20L,
21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L,
34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 52L, 53L, 54L, 55L, 57L, 59L, 60L, 61L, 62L, 63L,
64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L,
77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 87L, 89L, 90L, 91L,
92L, 93L, 94L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L,
105L, 106L, 107L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L,
117L, 118L, 119L, 120L, 121L, 123L, 124L, 125L, 126L, 127L, 128L,
130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L,
141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L,
152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L,
163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L,
174L, 175L))
答案 0 :(得分:1)
请勿使用scale_*()
设置限制。用coord_*()
进行设置。
第一个函数(家族)删除行;第二个功能是放大视角。
edit :这是@teunbrand的评论。将第四行替换为
j <- j %+%
scale_y_continuous(
expand=c(0,0),
name = "XX",
breaks=yaks,
# limits = c(0, 8.5), # This is the undesirable specification
sec.axis = sec_axis(~ . * 1 , name = "YY", breaks=yaks, labels=c("0.000","0.025","0.050","0.075","0.100","0.125","0.150", "0.175","0.200", "0.225"))
) +
coord_cartesian(ylim=c(0, 8.5))