Adjust the font of legend in R - r

I use legend() to produce a legend shown below
The text lies beyond the plot box. I tried to use cex = to adjust the box, however, it can only adjust the size of the whole box, but have nothing to do with the text font.
Is there anyway to make the text font smaller?
Here is my sample code:
legend("bottomleft", legend = c("Simulated", "Estimated/Predicted
Median", "95% Credit Intervals"),
col = c("gray35", "red", "red"), lty = c(1, 1, 2),
lwd = c(3, 2, 1),
text.font = 3, inset=.02, bg='gray90')

You can set graphical parameters by applying par(). For example:
plot(c(1:4), c(1:4), type = 'l')
par(cex = 1) #set legend font to 1
legend("topleft", legend="a line", lty = 1)

If you set bty="n" it won't draw a box
legend("bottomleft", legend = c("Simulated", "Estimated/Predicted
Median", "95% Credit Intervals"),
col = c("gray35", "red", "red"), lty = c(1, 1, 2),
lwd = c(3, 2, 1),
text.font = 3, inset=.02, bg='gray90',bty="n")

Try to hold the pt.cex parameter to 1, while trying different values for cex inside the legend call. pt.cex control the size of points and lines of the legend.
x <- rnorm(100, 10, 4)
y <- rnorm(100, 10, 4)
plot(x, y, type = "n")
## I tried to feed cex with 1.1 and 0.4. The font size changes while the lines remain unchanged.
legend("bottomleft", legend = c("Simulated", "Estimated/Predicted
Median", "95% Credit Intervals"),
col = c("gray35", "red", "red"), lty = c(1, 1, 2),
lwd = c(3, 2, 1),
text.font = 3, inset=.02, bg='gray90', pt.cex = 1, cex = 0.4)
As you can see, the size size of the font change while the lines remain almost the same. Try play with them until you do not find the correct proportions for your plot.

Related

Making a dark theme plot in R

Let say I want to draw this function f(x) = x^2. I want the graph of the function to be any color I want and everything else (except the axis) black. Is this possbile?
graphics.off()
par(bg = "black")
curve(x^2, -2, 2, col = "green", axes = FALSE, ann = FALSE)
title("my plot", xlab = "X", ylab = "X^2", col.lab = "white", col.main = "white")
box(col = "white")
axis(1, seq(-2, 2, 1), seq(-2, 2, 1), col = "white", col.axis = "white")
axis(2, seq(0, 4, 1), seq(0, 4, 1), col = "white", col.axis = "white")
Maybe you can ?plot to see how to make it. Below is just an example
plot(x<-seq(-5,5,0.1),
x**2,
main="square function",
ylab="f(x)=x^2",
type="l",
col=28)

How to customise dates (years) on the x-axis in R

I am struggling to customise the jump size on the x-axis in R.
Current code:
par(mfrow = c(2,2))
r.star.ts.sp <- ts(r.star.sp, frequency = 4, start = c(1978,1), end = c(2018, 1))
# Big drop in r* around 123th quarter equivalent to 2008:Q4 / 2009:Q1
trendgrowth.ts.sp <- ts(trendgrowth.sp, frequency = 4, start = c(1978,1), end = c(2018, 1))
plot.ts(r.star.ts.sp,
ylim = c(-3, 4), xlab = " ", ylab = " ", axes = F, col = "blue")
lines(trendgrowth.ts.sp, lty = 2, col = "red")
abline(h = 0, lty = 2)
title(main ="r* and Trend Growth", line = 0.5, font.main = 3)
box()
axis(4)
axis(1)
legend("bottomleft", legend = c("r*", "Trend Growth (g)"),
bty = "n", lty = c(1,2), col = c("blue", "red"), horiz = F, text.col = "black",
cex = 1, pt.cex = .5, inset = c(0.02, 0.02))
# -------------------------------------- #
# Plot output gap and real rate gap
# -------------------------------------- #
outputgap.ts.sp <- ts(outputgap.sp, frequency = 4, start = c(1978,1), end = c(2018, 1))
realrategap.ts.sp <- ts(realrategap.sp, frequency = 4, start = c(1978,1), end = c(2018, 1))
plot.ts(outputgap.ts.sp, ylim = c(-20, 15), xlab=" ", ylab=" ", axes = F, col="blue")
lines(realrategap.ts.sp, lty = 2, col = "red")
abline(h = 0, lty = 2)
legend("topright", legend = c("Output Gap", "Real Rate Gap"),
bty = "n", lty = c(1,2), col = c("blue", "red"), horiz = F, text.col = "black",
cex = 1, pt.cex = .5, inset = c(0.02, 0.02))
title(main = "Output Gap and Real Rate Gap", line = 0.5, font.main = 3)
box()
axis(side = 4)
axis(side = 1)
How would one specify the years on the x-axis from 1975 to 2020 with jumps of 5 years?
Furthermore, (off-topic) I need two plots next to each other, but I feel that par(mfrow = c(2,2)) is not the correct statement. However, changing it into par(mfrow = c(1,2)) creates abnormal large figures.
Thanks!
The OP has requested to specify the years on the x-axis from 1975 to 2020 with jumps of 5 years.
This can be achieved by
axis(1, at = seq(1975L, 2020L, by = 5L))
However, the result may depend on the mfrow parameter. Here is a a dummy example using par(mfrow = c(2, 2)):
Note that the x-axis of the left graph was created by axis(1) while the x-axis of the right graph was created by axis(1, at = seq(1975L, 2020L, by = 5L)). Also note the large white space below the two graphs.
With par(mfrow = c(1, 2)) the result becomes
Here, the right graph shows unlabeled ("minor") tick marks. This is explained in the mfrow section of ?par: In a layout with exactly two rows and columns the base value of "cex" is reduced by a factor of 0.83. So, font size is reduzed by 17% per cent which allows to label all tick marks without overplotting.

R specifying point border color differently from fill and line

This is related to points border color and line color is different between legend box and whole plot box when pch=21
I have a plot with filled point (pch=21), black border and green filled, but I also have whiskers for the error that are the same color as the fill color. I know I can match the point in the legend using a combination of "col" and "pt.bg", but then the line color matches the point border, which is not what I need:
x=1:10
y=runif(10)
plot(x,y,lwd=3,col="black",bg="green",pch=21,cex=2)
arrows(x,y-0.05,x,y+0.05,lwd=3,col="green",angle=90,code=3)
legend(5,0.8,col="black",pt.bg="green",lwd=3,pch=21,legend="text",cex=2)
gives the following...
Is there a way of having legend green lines and green filled points with the point border black? Even nicer would be having the whiskers, but I think that is probably not possible...
Make two legend calls. One to plot the line with bg = NA and the other to plot the point with bty = "n":
set.seed(1)
x=1:10
y=runif(10)
plot(x, y, lwd = 3, col = "black", bg = "green", pch = 21, cex = 2)
arrows(x, y-0.05, x, y+0.05, lwd = 3, col = "green", angle = 90, code = 3)
legend("topright",
col="green",
lwd = 3,
lty = 1,
legend = "text",
cex = 2,
bg = NA)
legend("topright",
col = "black",
pt.bg = "green",
pch = 21,
lwd = 3,
legend = "text",
cex = 2,
lty = 0,
bty = "n")

Is it possible to change the shape of the "images" in the legend of an igraph plot?

Using:
legend(x=0, y=-1.2, xjust = 0.5, ncol=2,
c("men", "women"), pch=21, col="black",
pt.bg=c("gray", "gray"), pt.cex=1.5)
I get this legend on an igraph plot:
However I need the shape of "men" to be a square. Is that possible? And if so then how?
If you set the parameter pch = 22 the legend will display squares instead of circles. For different symbols use pch = c(21, 22). For more information on controlling the plotting character, type ?pch in the console.
Control the legend shapes as you would colors, text....
legend(x = 10, y = 100, xjust = 0.5, ncol = 2, c("men", "women"),
pch = c(22, 21),
col = "black", pt.bg = c("gray", "gray"), pt.cex = 1.5)

r- Unclear abbreviations of cases' labels on plot

I have a problem with display of abbreviated cases' names on the following plot.
The code I use:
install.packages("HSAUR2")
library(HSAUR2)
data("Forbes2000", package="HSAUR2")
head(Forbes2000)
attach(Forbes2000)
not_na<-subset(Forbes2000,complete.cases(Forbes2000)
order_profits<-(order(not_na$profits))
top50<-not_na[order_profits[1946:1995], c("name","sales","profits", "assets")]
plot(top50$sales~top50$assets,col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 1)
top50$assets,top50$sales, abbreviate(name,1, strict="TRUE",named="FALSE"), cex= .7,pos=4)
I tried to make it differently:
plot(top50$sales~top50$assets,col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 1)
ab<-abbreviate(name,1, strict="TRUE",named="FALSE")
text(top50$assets,top50$sales, labels=ab, cex= .7,pos=4)
But had the same result.
If I use full labels instead, the display is ok, but plot gets messy:
plot(top50$sales~top50$assets,col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 1)
text(top50$assets,top50$sales, labels=top50$name, cex= .7,pos=4)
Could you show, how to make abbreviations look well? Is there any mistake in my code or the problem is due to soft issues?

Resources