2012-02-20 32 views
3

我想一個傳說附加到R. 一個情節我嘗試下面的代碼(從http://www.harding.edu/fmccown/r/拍攝)如何結合情節和傳奇?

# Define cars vector with 5 values 
cars <- c(1, 3, 6, 4, 9) 

# Define some colors ideal for black & white print 
colors <- c("white","grey70","grey90","grey50","black") 

# Calculate the percentage for each day, rounded to one 
# decimal place 
car_labels <- round(cars/sum(cars) * 100, 1) 

# Concatenate a '%' char after each value 
car_labels <- paste(car_labels, "%", sep="") 

# Create a pie chart with defined heading and custom colors 
# and labels 
pie(cars, main="Cars", col=colors, labels=car_labels, 
    cex=0.8) 

# Create a legend at the right 
legend(1.5, 0.5, c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, 
    fill=colors) 

然而,這並不工作非常好。在餅圖(汽車,主=「汽車」,col =顏色,標籤= car_labels,cex = 0.8)行後,情節顯示爲沒有圖例:-) .......我看到的每個示例互聯網似乎有繪圖功能後傳說功能,所以看起來很怪異..............

當我嘗試執行傳說功能我得到

圖例(1.5,0.5,c(「Mon」,「Tue」,「Wed」,「Thu」,「Fri」),cex = 0.8, + fill = colors) strwidth(圖例,單位=「user」,cex = cex): plot.new尚未被調用

回答

5

您離開座標系。試試這個

# Create a legend at the right 
legend("topleft", c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=colors) 

產生如下圖:

pie chart

legend不同的放置選項查看幫助頁面。

1

我認爲1.5, 0.5的位置將它從頁面上刪除。嘗試

legend("right", c("Mon","Tue","Wed","Thu","Fri"), cex=0.8, fill=colors) 

pie函數之後它顯示爲沒有圖例; legend函數將圖例添加到當前繪圖。

PS。你也可以考慮其他類型的情節。餅圖因視覺上的誤導而臭名昭着。