1

I try to make a plot similar to the top three plot of this:

enter image description here

I found a partial answer here, however I am unsure how to add the p-values in the scatter-plot.

Any tips?

Community
  • 1
  • 1
user1261558
  • 369
  • 1
  • 2
  • 11
  • `?text`? For specific help, please give a [reproducible example](http://stackoverflow.com/a/5963610/1274516) – blmoore Sep 27 '13 at 12:03
  • I had a similar question a while ago, check this out: [Avoiding overlap when jittering points](http://stackoverflow.com/questions/11889353/avoiding-overlap-when-jittering-points). Then add the test results with `segments` and `text`. – Backlin Sep 27 '13 at 12:05
  • Backlin: Aha, thank you! – user1261558 Sep 27 '13 at 12:34

1 Answers1

1

You've already got a partial answer. If you just want to know how to put p-values on then use text. (looking at graph C).

text(x = 1.5, y = 73, 'p = 0.03')

If you want the p-values and the lines underneath, assuming you also want those caps on the lines, use arrows instead of segments.

arrows(1, 70, 2, length = 2, angle = 90, code = 3)

If you're sticking with solving this in base R that's a great learning exercise and can give you full control over your plot. However, if you just want to get it done I'd suggest the beeswarm package (you're making beeswarm plots).

As an aside, this prompted me to investigate why you get those upward curving lines in beeswarm plots. It's a consequence of the typical algorithm. The line curves upward because the positions are calculated through increasing y-values. If the next y-value is so close that the points would overlap in the y-axis it's plotted at an angle off the x position. Many points close together on Y results in upward curving lines until you get far enough along Y to go back to X. Smaller points should alleviate that. Also, the beeswarm package in R has several optional algorithms that avoid that as well.

John
  • 23,360
  • 7
  • 57
  • 83