图例是页面(R)中错误位置的豌豆:未完全显示

时间:2019-06-14 09:36:13

标签: r data-visualization legend

我已经在R中的绘图中添加了以下命令:

df<- read.table("filename.csv", header=TRUE, sep=",", stringsAsFactors=FALSE)
tdf=as.data.frame(df[2:ncol(df)])

# draw the plot
bb<- barplot(as.matrix(tdf), beside=T ,
             col=colours,border="black", 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)

但是,图例显示在图的外部,但没有完全显示,

我希望它可以在情节的右侧之外看到。我不明白这里的位置 enter image description here

1 个答案:

答案 0 :(得分:0)

如果您将title函数"topleft"更改为"topright"并删除了inset自变量,则标题应为OK。 请参见下面的代码:

# simulation
set.seed(123)
tdf <- as.data.frame(matrix(rbinom(20, 15, .4) * 8, ncol = 4))

# draw the plot
colours <- 2:6
bb <- barplot(as.matrix(tdf), beside=T ,
             col=colours,border="black", 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("topright", c("M1","M2","M3","M4","M5", "M6"), cex=0.6,xpd=TRUE,  fill=colours)

输出: Output