条形图和线条图的图例组成

时间:2017-09-14 20:51:55

标签: r bar-chart legend

我有一些气象数据,我正在绘制一个显示月平均值的时间序列。我在同一块画布上有降雨作为条形图和温度作为线条图。我需要生成一个带有不同符号的图例,即用于降雨的彩色框和用于温度的两条彩色线条。

这是数据

   >  month    max_temp    min_temp     rain        humid
    >     Jan   24.65032258 12.54193548 1.425806452 75.44064516
    >     Feb   25.65248227 13.39219858 1.876595745 79.06666667
    >     Mar   24.26129032 12.41354839 2.318709677 83.55806452
    >     Apr   21.038      10.62933333 5.013333333 90.026
    >     May   17.29548387 7.16516129  4.080645161 91.83225806
    >     Jun   14.98733333 5.529333333 4.4         91.2
    >     Jul   13.98516129 4.06516129  3.987096774 90.60322581
    >     Aug   15.18258065 5.316129032 3.350322581 89.26129032
    >     Sep   16.434      7.668       4.229333333 84.03666667
    >     Oct   18.13225806 8.481290323 2.277419355 81.26129032
    >     Nov   20.07666667 9.558666667 2.562       75.99266667
    >     Dec   22.51032258 12.13225806 2.296774194 76.28193548

这是我生成情节的代码

weather<-weather
month_number<-seq(1,12,1)
months<-months[1:12]
new<-data.frame(rain=c(weather$rain),order=c(months))
graph<-barplot(height=new$rain, names.arg=new$order,col="light blue",
  cex.axis=0.2,border=NA,xaxt='n',yaxt="n",xlab="",ylab="",ylim=c(0,10))
axis(side=1, pos=0,tck=-0.05,at=graph, labels=months[1:12],cex.axis=0.7,las=2,font.axis=2)
axis(4,las="1",cex.axis=0.7,font.axis=2)
mtext("Accumulated rainfall (mm)",side=4,line=2,cex=0.7)
par(new=TRUE)
weather$x<-month_number
plot(weather$x,weather$max_temp,type="l",las=1,col="red",lwd=1.5,xaxt="n",
     ylim=c(0,30),xlab=NA,ylab = expression(paste("Temperature ",degree,"C")),
     ,cex.lab=0.8,cex.axis=0.7,font.axis=2)
lines(weather$x,weather$min_temp,col="blue",lwd=1.5)
legend("bottom",inset=c(0,1.05),legend=c("Rainfall", "Max temp", "Min temp"),xpd=TRUE,
       ,horiz=TRUE,pch=c(15),col=c("light blue","red","blue"),
       lty=c(0,1,1),lwd=2,title=NA,cex=0.6,bty='n')

情节本身。在图例中,我想增加降雨量字符的大小,并省略两个温度线条中显示的情节字符。字体大小需要保持不变。我已经尝试了pch=26seen plots a null character,但收到了错误消息。另外looked here但它没有解决我的问题。

Weather data

1 个答案:

答案 0 :(得分:0)

以下是如何省略行中的绘图字符(例如使用pch=c(19,NA,NA)):

legend("bottom",inset=c(0,1.05),legend=c("Rainfall", "Max temp", "Min temp"),xpd=TRUE,
       ,horiz=TRUE,pch=c(19,NA,NA),col=c("light blue","red","blue"), 
       lty=c(0,1,1),lwd=2,title=NA,cex=0.6,bty='n')

为了使情节字符不在线条中并使降雨情节字符更大,我实际上会使用fill

legend("bottom",inset=c(0,1.05),legend=c("Rainfall", "Max temp", "Min temp"),xpd=TRUE,
       ,horiz=TRUE,col=c("light blue","red","blue"), fill=c("light blue", NA,NA), border = c("light blue", NA,NA),
       lty=c(0,1,1),lwd=2,title=NA,cex=0.6,bty='n')