如何解决R中barplot中图例的问题?

时间:2019-07-14 11:20:35

标签: r bar-chart

我是R语言的初学者,我正在努力从csv文件(myfile.csv)中的表绘制条形图

     FC      TC      PC
M1  82.8    83.9    73.9
M2  84.96   84.74   79.16
M3  87.8    80.85   76.7
M4  89.4    88.25   
M5  97.79   97.79   96.37
M6  98.49   98.23   97.4

在编写以下代码后,图例的排列不正确,并且无法完全显示。

library(RColorBrewer)   
display.brewer.all()
#display.brewer.pal(n = 10, name = "PRGn")
# Open a pdf file
pdf("base.pdf") 

# set the colors

colours<-brewer.pal(n=6,name = "YlOrRd")
# read csv file as dataframe
df<- read.table("myfile.csv", header=TRUE, sep=",", stringsAsFactors=FALSE)
tdf=as.data.frame(df[2:ncol(df)])

bb<- barplot(as.matrix(tdf), beside=T ,
             col=colours,border="white", ylim=c(0,100), ylab="Percentage (%)",xlab="Methods")

y<-as.matrix(tdf)
text(bb,y+2,labels=as.character(y),pos =1,offset=3,cex = 0.6, col = "black")  

legend("topleft", c("M1","M2","M3","M4","M5", "M6"), cex=0.6,inset=c(1,0),xpd=TRUE,  fill=colours)
dev.off()

这是输出,图例落在小节图和图像边界的外面。

enter image description here

如何解决此问题?

0 个答案:

没有答案