JoeStat1986
JoeStat1986

Reputation: 79

Legend Horizontal Graph Spacing in R

Ok so I actually asked this before, although I have made more progress now, but I wasn't sure how to add code to a comment of my old post. Anyways here is my new problem. I am using the legend command to creat a horizontal leged and I was hoping there is a way to reduce the space between the legend item list

# initial variables, this is fine
t = 0:50
int_seq = seq(0,0.1,by=0.025)
int_seq100 = 100 * int_seq
colors = c("black","red","blue","green","orange")
index = 1:length(int_seq)

#AV Simple Interest, this is fine
avSimple = function(i,t){
    av = (1 + (i * t))
    return(av)}

# Plot range for y-axis, this is fine
yrange = c(avSimple(min(int_seq),min(t)) * 0.95,
    avSimple(max(int_seq),max(t)) * 1.15)

# Plots Simple Interest with different interest rates, this is fine
plot(t,avSimple(int_seq[1],t), type="l", main = "AV Simple Interest",
    xlab = "Time", ylab = "AV", ylim = yrange)
for (i in index) 
    lines(t,avSimple(int_seq[i],t), col = colors[i])

# Adds legend to plot for different interest rates
# where the trouble is
# ? And how can I reduce distance between each index in the labs string
# so it is only a space or two apart from each other
labs = c("i =:",sprintf('%s%%', c(int_seq100)))
legend('topleft', legend = labs, text.col = c("black",colors), 
    horiz = TRUE, bty = "n")

Upvotes: 2

Views: 2855

Answers (2)

Sathish
Sathish

Reputation: 12723

Data

t = 0:50

int_seq = seq(0,0.1,by=0.025)

colors = c("black","red","blue","green","orange")

mydf <- data.frame(rep(NA, length(t)), stringsAsFactors = FALSE)

for(i in int_seq){   mydf <- cbind(mydf, data.frame(1+(i*t), stringsAsFactors = FALSE)) }

mydf <- mydf[, 2:ncol(mydf)]

colnames(mydf) = paste(int_seq*100, "%", sep = "")

plot lines from a data frame

matplot(mydf, type = "l", col = colors, lty = 1, lwd = 2)

legend('topleft', lty = 1, lwd = 2,  legend = colnames(mydf), text.col = colors, horiz = TRUE, bty = "n", cex = 0.75, col = colors)

plot points from a data frame

matplot(mydf, type = "p", col = colors, pch = c(seq(20, along.with = colnames(mydf))))

legend('topleft', legend = colnames(mydf), text.col = colors, box.col = "black", horiz = FALSE, bty = "o", cex = 0.75, col = colors, pch = c(seq(20, along.with = colnames(mydf)))) 

plot points and lines from a data frame

matplot(mydf, type = "p", col = colors, pch = c(seq(20, along.with = colnames(mydf))))

for(i in 1:ncol(mydf)){lines(mydf[,i], col = colors[i])}

legend('topleft', lty = 1, lwd = 2,  legend = colnames(mydf), text.col = colors, horiz = TRUE, bty = "n", cex = 0.75, col = colors, pch = c(seq(20, along.with = colnames(mydf))))

Upvotes: 2

Ruthger Righart
Ruthger Righart

Reputation: 4921

The text.width comment can do that. You need to play around with the numbers. I also changed to smaller font size (cex) and set all your numbers to 2 decimals (sprintf). The last lines in your script should be changed with the following:

labs = c("i =:",sprintf('%.2f', c(int_seq100)))
legend('topleft', text.width=c(0.3, 0.3, 0.3, 0.3, 0.3), cex = 0.75, legend = labs, text.col = c("black",colors), 
horiz = TRUE, bty = "n")

enter image description here

Upvotes: 1

Related Questions