Reduce the distance between plot title and map [duplicate] - r

I have been unable to find a way to adjust the (vertical) distance between plot and main title in R using par. In this example:
plot(1, 1, main = "Title")
I can adjust the position of the axis titles using:
par(mgp = c(2.5, 1, 0))
But I see no way to similarly adjust the main title. I am aware that more manual control is possible using title or mtext, but I assume that there is a way setting the title distance using par as well, which would be more elegant for my purposes.

We can use title() function with negative line value to bring down the title.
See this example:
plot(1, 1)
title("Title", line = -2)

To summarize and explain visually how it works. Code construction is as follows:
par(mar = c(3,2,2,1))
barplot(...all parameters...)
title("Title text", adj = 0.5, line = 0)
explanation:
par(mar = c(low, left, top, right)) - margins of the graph area.
title("text" - title text
adj = from left (0) to right (1) with anything in between: 0.1, 0.2, etc...
line = positive values move title text up, negative - down)

Try this:
par(adj = 0)
plot(1, 1, main = "Title")
or equivalent:
plot(1, 1, main = "Title", adj = 0)
adj = 0 produces left-justified text, 0.5 (the default) centered text and 1 right-justified text. Any value in [0, 1] is allowed.
However, the issue is that this will also change the position of the label of the x-axis and y-axis.

Related

Problems saving correlation plot: dimension [duplicate]

I have been unable to find a way to adjust the (vertical) distance between plot and main title in R using par. In this example:
plot(1, 1, main = "Title")
I can adjust the position of the axis titles using:
par(mgp = c(2.5, 1, 0))
But I see no way to similarly adjust the main title. I am aware that more manual control is possible using title or mtext, but I assume that there is a way setting the title distance using par as well, which would be more elegant for my purposes.
We can use title() function with negative line value to bring down the title.
See this example:
plot(1, 1)
title("Title", line = -2)
To summarize and explain visually how it works. Code construction is as follows:
par(mar = c(3,2,2,1))
barplot(...all parameters...)
title("Title text", adj = 0.5, line = 0)
explanation:
par(mar = c(low, left, top, right)) - margins of the graph area.
title("text" - title text
adj = from left (0) to right (1) with anything in between: 0.1, 0.2, etc...
line = positive values move title text up, negative - down)
Try this:
par(adj = 0)
plot(1, 1, main = "Title")
or equivalent:
plot(1, 1, main = "Title", adj = 0)
adj = 0 produces left-justified text, 0.5 (the default) centered text and 1 right-justified text. Any value in [0, 1] is allowed.
However, the issue is that this will also change the position of the label of the x-axis and y-axis.

Add second barplot to existing one manually (using add=T)

Is there a way in base R to manually add a second barplot to an existing one. I know how to do it if the two series are from the same data object (using barplot( ... beside=T)) or I guess one could draw rectangles (rect(...)) which barplot wraps. If your data is from different objects how can you then do it with the barplot function ? How to control bar positions?
I tried this using the space parameter (obviously not working):
h1 <- c(10,5,1)
h2 <- c(8, 3, 1)
barplot(h1, width = 0.5, space = 2, col='red')
barplot(h2, width = 0.5, space = 2.5, col='blue', add=T)
It is impossible to get the bars besides each other as when using the beside=T argument.
Desired output is something along this:
barplot(matrix(c(h1, h2), nrow=2, byrow=T), beside=T, col=c('red', 'blue'))
UPDATE: how it works
In order for me to finally - I hope - understand the width and space arguments, we can plot an axis and play with the parameters for the blue data.
barplot(h1, width = 0.5, space = 2, col='red')
axis(1, seq(0, 10, 0.5)) #way out of the plot region
barplot(h2, width = 0.25, space = c(4,2,4), col='blue', add=T)
From this it seem as (correct me if I am wrong):
1. width is the width of each bar - recycled as necessary
2. space controls the space to the previous bar (to the left) or to 0 for the first bar, and is calculated as width*space for the current bar - recycled as necessary. So the first blue bar starts at (space to 0) 0.25*4 = 1 and its right side is at 1+0.25 = 1.25; the second bar starts at 1.25+0.25*2 = 1.75, and its right side is at 1.75+0.25 = 2. And so forth...
You can do this:
h1 <- c(10,5,1)
h2 <- c(8, 3, 1)
barplot(h1, width = 0.5, space = 2, col='red')
barplot(h2, width = 0.5, space = c(3,2,2), col='blue', add=T)
And this will be the output:

Adding a point to the left of an mtext() in R

I was wondering if I could insert a points() to appear to the left of an mtext()? In other words, is there a way I can get the x, y of the mtext() so I can determine the right place for this points() to appear?
Here is my R code:
curve(dnorm(x),-3,3)
mtext(bquote(paste("Medium: ",bold('CT'[12])," = ", .(round(1/3,3)))),line=3)
It becomes pretty easy if you use text instead of mtext as you can use x and y for both text and points.
#Plot the curve
curve(dnorm(x),-3,3)
#Enable drawing outside the plot region
par(xpd = TRUE)
#STEP 3. Add text at certain x and y.
text(x = 0, y = 0.45,
bquote(paste("Medium: ",bold('CT'[12])," = ", .(round(1/3,3)))))
#Determine the width of the text you added
text_width = strwidth( bquote(paste("Medium: ",bold('CT'[12])," = ", .(round(1/3,3)))) )
#Find out x poistion just left of the text.
#Since the text is centre aligned by default,
#you can subtract half the text_width to the x value
#that you had used to add text in STEP 3
#You may also add 10% extra space
points_x = (0 - text_width/2) - (0.1*text_width)
#Add a point just to the left of the text
points(x = points_x, y = 0.45, pch = 20, cex = 3)

Adjust plot title (main) position

I have been unable to find a way to adjust the (vertical) distance between plot and main title in R using par. In this example:
plot(1, 1, main = "Title")
I can adjust the position of the axis titles using:
par(mgp = c(2.5, 1, 0))
But I see no way to similarly adjust the main title. I am aware that more manual control is possible using title or mtext, but I assume that there is a way setting the title distance using par as well, which would be more elegant for my purposes.
We can use title() function with negative line value to bring down the title.
See this example:
plot(1, 1)
title("Title", line = -2)
To summarize and explain visually how it works. Code construction is as follows:
par(mar = c(3,2,2,1))
barplot(...all parameters...)
title("Title text", adj = 0.5, line = 0)
explanation:
par(mar = c(low, left, top, right)) - margins of the graph area.
title("text" - title text
adj = from left (0) to right (1) with anything in between: 0.1, 0.2, etc...
line = positive values move title text up, negative - down)
Try this:
par(adj = 0)
plot(1, 1, main = "Title")
or equivalent:
plot(1, 1, main = "Title", adj = 0)
adj = 0 produces left-justified text, 0.5 (the default) centered text and 1 right-justified text. Any value in [0, 1] is allowed.
However, the issue is that this will also change the position of the label of the x-axis and y-axis.

Adding label for last point of line in R plot

Consider the following plot:
par(xaxs='i',yaxs='i')
q1 <- c(1000000.0, 908364.8, 876009.1, 847892.8, 824808.3, 805416.2, 785266.2, 770997.1, 753908.6, 744599.9, 706777.6, 674659.9, 634654.4, 601440.4, 568259.7, 535361.3, 493679.9, 465526.5, 429766.6, 395244.7, 361483.2, 332136.6, 308574.5, 285500.6, 262166.2 ,237989.0 , 210766.1, 188578.1, 166762.3 , 140399.8 ,114865.5)
plot(q1, type = "l", lty = 1, lwd = 2, col = "darkolivegreen3", ylim = c(0,4*10^6), xlim = c(1,30), bty = "l")
text(30, q1[30], labels = "text", col = "gray36", cex = 0.8, pos = 4)
I would like to add the label "text" at the right of the last point of the green line (i.e. the point on the line with x = 30).
I tried the code above but the text doesn't show up! Any ideas how to solve that?
Thanks!
By default things in a plot are clipped to the plot region, you are not seeing the text because it has been clipped. You can either use the mtext function to explicitly place the text into the margin. Or if you specify par(xpd=NA) then the clipping will be turned off (well it will still clip to the device region) and the text plotted using the text function will now be plotted extending into the margin. Either way you will probably want to specify some space in the appropriate margin so there is room for the text to be and look nice. See ?par for how to specify the margin and more detail on clipping.
I just realized that this could be done using mtext:
mtext("text", side = 4, at = q1[30], las = 1)

Resources