0

I need help to connect a geom_segment to a geom_point outside the plot. I tried looking up answers to similar questions but that did not solve my problem.

I have a plot with two y-axes as shown below:

enter image description here

I need to add an orange point at (15, 120). As you can see, each orange dot is connected with an orange geom_segment. However, geom_segment cannot draw outside the plot. I tried adding coord_cartesian(ylim = c(0, 120), xlim = c(0,25), clip="off") but that did not help. Maybe it has something to do with the double-y-axis?

My script is the following and My data can be found below.

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,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4L, 5L, 3L, 12L, 5L, 4L, 3L, 
2L, 4L, 3L, 4L, 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))
1
  • 1
    Sorry I'm not answering your question directly, but thought this tip may be of use to you. Instead of having a geom_point() or geom_segment() line for every point/segment, you can put your variables in a data.frame where columns correspond to your mapping variables and plot all your points/segments at once by referring to the column names inside the aes() mapping. Commented May 6, 2019 at 19:19

1 Answer 1

1

Don't set the limits with scale_*(). Set it with coord_*().

The first function (family) removes rows; the second functions zooms the perspective.

edit: This follows @teunbrand's comment. Replace the fourth line with

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))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the quick response. I believe I tried that. Can you write out the line based on the script I uploaded?
Add j + coord_cartesian(ylim = c(0, 8.5)); remove the limits = c(0, 8.5) inside the scale_y_continuous() function.
I spelled it out, following @teunbrand's comment. Stepping back a minute, consider avoiding all these hard coded commands, and use a second dataset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.