Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

data visualization - Legend is apears in the wrong place in the page (R): not fully shown

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...