【发布时间】:2016-08-24 21:17:06
【问题描述】:
我试图在 R 中使用两个 y 轴绘制多年来的一些数据。但是,每当我尝试包含图例时,图例都会主导我的情节。当我使用其他地方建议的解决方案时,如keyword 和/或使用cex 参数,在另一篇帖子here 中建议,它要么变得不可读,要么仍然太大。
这是我随机生成数据的示例:
#Create years
year.df <- seq(1974, 2014, 1)
# Create y-axis data
set.seed(75)
mean1 <- rnorm(length(year.df), 52.49, 0.87)
mean2 <- rnorm(length(year.df), 52.47, 0.96)
#Create dataframe
df <- data.frame(cbind(year.df, mean1, mean2))
我想要第二个 y 轴,多年来这两种方式的差异
df$diff <- abs(df$mean1 - df$mean2)
当我使用下面的代码创建两个 y 轴进行绘图时:
par(mfrow=c(1,1), mar=c(5.1,4.1,4.1,5.1))
with(df, plot(year.df, mean1, type = "l", lwd=4, xlab="Year", ylab="Mean", ylim=c(48,58)))
with(df, lines(year.df, mean2, type = "l", col="green", lwd=4))
par(new=TRUE)
with(df, plot(year.df, diff, type="l", axes=FALSE, xlab=NA, ylab=NA, col="red", lty=5, ylim=c(0,10)))
axis(side = 4)
mtext(side = 4, line = 3, "Annual Difference")
legend("topleft",
legend=c("Calculated", "MST", "Diff"),
lty=c(1,1,5), col=c("black", "green", "red"))
当我在legend() 中使用cex=0.5 参数时,它开始变得不可读:
有没有办法以清晰易读的方式格式化我的图例?比我有的更好?
【问题讨论】:
-
(a) 如果您保存绘图,文本的大小将取决于您保存的文件的尺寸。 (b) 您是否尝试过其他
cex值?如果cex = 0.5太小,也许cex = 0.65正好? (c) 你的传说占据了相当多的情节空间。最好直接用text()标记线条,让 y 轴上升到 55 和 7,这样您的数据会占用更多的绘图空间。 -
我无法重现您的情节:我情节中的图例位于左上角,带有短线和可读文本。不过,旁注;你用过
cex,pt.cex(而不是cex)会让事情变得更好吗? (即保持cex接近1,缩小pt.cex。)