0

Short background: I'm trying to create a custom axis for a plot in R, where the axis labels are numeric but do not correspond to the coordinate values they are placed at. I want R to format the labels in scientific notation because right now they're coming out like "10000000" which is not ideal.

Long background: I am making multi-panel figures where each panel is a filled contour plot. The filled.contour function will not make multi-panel figures, so I am using .filled.contour, which eliminates the legend - so I have been making legends manually. I use rect to create a stack of rectangles covering the color palette and axis to label values on the legend. However, the labels on the legend are based on the z-values, not the y-values, so I have an axis with numeric labels that don't match the coordinates the labels are placed at. When I feed those numeric values to axis manually, it stubbornly formats them NOT in scientific notation, which is suboptimal when the values are in the tens of millions.

This is the code as it's currently written:

breaks <- pretty(limits, n = 6)
          
marks <- (breaks - limits[1])/(limits[2] - limits[1]) # y-values in plot region go from 0 to 1
          
axis(side = 4, at = marks, labels = breaks, las = 1, tick = T, pos = 1.21, yaxs = "i", cex.axis = cex_legend, tcl = tickle)

Previously, I just adjusted the y-values of the contour plot to span the range of z-values (limits), so that the at and labels arguments matched:

axis(side = 4, at = pretty(limits, n = 6), labels = T, las = 1, tick = T, pos = 1.21, yaxs = "i", cex.axis = cex_legend, tcl = tickle)

and when I did this the labels were nicely formatted in scientific notation. (I would have had the same problem with unformatted labels on the actual y-axis, since the labels and coordinates didn't match, but since the values on the y-axis just go from 0 to 1, their formatting isn't an issue.) For reasons I won't go into, it is now preferable to use the 'real' y-values for the contour plot, but this leads to unformatted labels on the axis for the color legend.

If I was just making a few plots I could create the labels manually, but I am making tons of these figures with all different legends so I need to be able to automate it.

  • 1
    This question will be substantially easier to answer with 1) working example data provided as the output of `dput(data)`, 2) working code you are using to create the graph, and 3) an image with a mock up of how you'd like it the final graph to look. See [How to make a great R reproducible example](https://stackoverflow.com/a/5963610/) for more. – Ian Campbell Apr 14 '21 at 02:41
  • The `labels=` argument in `axis` accepts character vectors. Can you just format the numbers the way you want with `sprintf`, `formatC`, or `prettyNum`? Alternatively, this might help: [Use superscripts in R axis labels](https://stackoverflow.com/questions/10628547/use-superscripts-in-r-axis-labels). – dcarlson Apr 14 '21 at 03:41
  • @dcarlson - these functions were all new to me, but `formatC` worked perfectly. Thanks! – Mary Bushman Apr 14 '21 at 04:05

0 Answers0