'Line break in expression
Is it possible to produce a line break in the expression() function? The text in bold needs to be broken into at least 2 sentences.
plot (DAC~Chlo, data = brazilw, pch = 15, col = "red", cex = 0.5,
**main=expression("Fig. 3. Relationship between diffuse attenuation coefficient at 490 nm (K"[d]*") and chlorophyll concentration at three coral reef sites")**,
xlab = expression("Chlorophyll concentration (mg "*m^{-3}*")"),
cex.lab = 0.8, cex.main = 0.8, cex.axis = 0.8, font.main=1,
ylim = c(0,0.3), xlim = c(0,3.5), ylab = expression("K"[d]*"(m "*-1^{-1}*")"))
Any help would be appreciated!
Solution 1:[1]
I'm not sure if I'm missing something, your code snippet won't run since I don't have your data set, but just insert \n into your string wherever you want a newline.
Here's a simple plot with your title:
plot(x = 1:10, y = 1:10, main=expression("Fig. 3. Relationship between diffuse attenuation coefficient at 490 nm (K"[d]*") and chlorophyll concentration at three coral reef sites"))
Run it in R and your title stretches off the edge of the plot window. Now run it with some newlines added:
plot(x = 1:10, y = 1:10, main=expression("Fig. 3. Relationship between\n diffuse attenuation\n coefficient at 490 nm (K"[d]*")\n and chlorophyll concentration at three\n coral reef sites"))
The newlines are added, but wont all fit in the top margin, you change the mai parameter for that: par(mai = c(1,1,x,0.5)) where x is the size in inches of your top margin. Hope that helps.
Solution 2:[2]
The expression atop(x, y) means 'x over y (no horizontal bar)' (see ?plotmath).
This means we can use this as a kind of hack:
plot(x = 1,
main = expression(paste(atop('Fig. 3. Relationship between', 'case A and B'))))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bill Beesley |
| Solution 2 | MS Berends |

