2015-05-30 38 views
1

我繪製的圖例超出了繪圖區域。渲染時,圖例的右側部分被截斷。我怎樣才能減少繪圖的寬度,所以它會適合?縮小繪圖寬度,使圖例不會被截斷

R plot with truncated legend

# resultValues got 9 cols and 2 rows, colors is a vector with 9 colors 
# The maximal y value is 2400 
# Setting width to a higher value just streches everthing 
pdf("Lineplot.pdf", width = 10, height = 6) 
max_y <- max(resultValues) 
plot(resultValues$col1, type="o", col=colors[1], ylim=c(0,max_y), axes=FALSE, 
    ann=FALSE, lwd=1.5) 
axis(1, at=1:2, lab=c("2013", "2014")) 
axis(2, las=1, at=250*0:max_y) 
for (colname in colnames) { 
    lines(resultValues[[colname]], type="o", lwd=1.5, 
     col=colors[match(colname, names(resultValues))]) 
} 
box() 
par(xpd=TRUE, mar=par()$mar+c(0,0,0,6)) 
legend(2.05, 2500, legend=names(resultValues), ncol=1, cex=1, fill=colors, 
     xjust=0, xpd=TRUE) 
dev.off() 
par(mar=c(5, 4, 4, 2) + 0.1) 
+0

你嘗試改變'帕(MAR = C(5,4,4,2)+ 0.1)'?我希望利潤率能夠解決問題。 – Konrad

+0

我做了,但不幸在'plot()'調用之後。像@ bgoldst說的,它必須在它之前完成。 – user3992979

回答

2

您可以通過配置使用par(mar=...)更寬的右邊界解決問題。請注意,這必須在之前完成到主要的plot()呼叫,因爲在通過初始的plot()呼叫修復它們後,邊距不能被更改。

演示(使用合成數據,因爲你沒有提供resultValuescolors)(假設你已經設置你的顯卡設備,例如pdf()x11(),等等):

## store data 
resultValues <- data.frame(col1=c(40,70),col2=c(1710,2500),col3=c(580,280),col4=c(200,1050),col5=c(160,140),col6=c(260,10),col7=c(380,300),col8=c(380,600),col9=c(150,190)); 
colors <- c('red','orange','lightgreen','green','#00ff88','#0088ff','blue','purple','magenta'); 
colnames <- colnames(resultValues); 

## widen right margin 
par(mar=par('mar')+c(0,0,0,3)); 

## draw plot 
max_y <- max(resultValues); 
plot(resultValues$col1, type="o", col=colors[1], ylim=c(0,max_y), axes=FALSE, ann=FALSE, lwd=1.5); 
axis(1, at=1:2, lab=c("2013", "2014")); 
axis(2, las=1, at=250*0:max_y); 
for (colname in colnames) lines(resultValues[[colname]], type="o", lwd=1.5, col=colors[match(colname, names(resultValues))]); 
box(); 
legend(2.05, 2500, legend=names(resultValues), ncol=1, cex=1, fill=colors, xjust=0, xpd=TRUE); 

plot

+0

工作正常,我的問題是,我已經設置了邊緣_after_主要情節調用,而不是_before_它。 – user3992979

1

您也可以使用ggplot2。

resultValues <- data.frame(col1=c(40,70),col2=c(1710,2500),col3=c(580,280),col4=c(200,1050),col5=c(160,140),col6=c(260,10),col7=c(380,300),col8=c(380,600),col9=c(150,190)) 
#^borrowed from above :) 

resultValues = rbind(t(resultValues[1,]),t(resultValues[2,])) 
resultValues = data.frame(resultValues) 
resultValues$col = c(paste("col", 1:9, sep = "") , paste("col", 1:9, sep = "")) 
resultValues$year = c(rep("2013",nrow(resultValues)/2),rep("2014",nrow(resultValues)/2)) 
names(resultValues) = c("y_value","col","year") 

繪圖:

require("ggplot2") 

    ggplot(resultValues, aes(x = year, y = y_value, group = col)) + 
      geom_point(aes(color = col), size = 3.5,shape =1) + 
      geom_line(aes(color = col)) + 
      scale_x_discrete(expand = c(0.05,0.05)) + 
      xlab("Year") + 
      ylab("") + 
      ggtitle("Your Graph") + 
      theme_bw() + 
      theme( panel.grid.minor = element_blank() 
       , panel.grid.major = element_blank() 
       , legend.title=element_blank() 
       ) 

enter image description here

相關問題