Legend is apears in the wrong place in the page (R

2019-09-26 07:40发布

I have added the following command to the plot in 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)

However, the legend appears outside of the plot and not fully shown,

I want it to be seen outside right side of plot. I do not understand the positioning here enter image description here

1条回答
Lonely孤独者°
2楼-- · 2019-09-26 07:55

If you change in title function "topleft" to "topright" and remove inset argument you title should be indicated OK. Please see the code below:

# 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: Output

查看更多
登录 后发表回答