[英]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
我正在使用Legend命令创建水平分支,我希望有一种方法可以减少Legend项目列表之间的空间
# 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")
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))))
The text.width comment can do that. text.width注释可以做到这一点。 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).
我还更改为较小的字体大小(cex),并将所有数字设置为2个小数位(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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.