2016-05-12 20 views
5

Tôi đã tạo barplot sau đây, nhưng vấn đề là các truyền thuyết không có màu phù hợp và các lưới nằm ở phía trước các thanh. Tôi muốn đặt các cột bên phải trong các hộp tại chú giải, và đặt lưới phía sau các thanh.Thêm màu vào chú thích

Tôi cũng muốn đánh dấu vào cuối mỗi thanh chỉ để xác định chúng

Làm cách nào để tôi có thể thực hiện các tính năng này trong R?

barkplot

Kịch bản r mà tôi sử dụng:

autos_data <- read.table("data.dat", header=T, sep="\t") 

barplot(rep(NA,length(autos_data)),ylim=c(0,max(autos_data)),axes=FALSE) 
barplot(t(as.matrix(autos_data)), main=NULL, ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50), beside=TRUE, lwd=1:2, angle=c(45,135), density=seq(5,40,10), col=c("blue","red","black","darkgreen")) 
grid(NA, 5, lwd=1,lty=5, col="gray") # grid only in y-direction 

# Place the legend at the top-left corner with no frame 
coords="top" 
legend(coords, c("WestVirginia","Chicago I","Chicago II","California"), text.col=par("col"), cex=1.2, bty="n", xpd=TRUE, horiz=TRUE, inset=c(0,-.22), angle=c(35,135,45,135), density=seq(5,40,10),col=c("blue","red","black","darkgreen")) 

Edit:

dput(autos_data) 
structure(list(WestVirginia = c(29L, 29L, 23L, 23L), ChicagoI = c(30L, 
21L, 36L, 26L), ChicagoII = c(39L, 42L, 35L, 46L), California = c(2L, 
8L, 6L, 5L)), .Names = c("WestVirginia", "ChicagoI", "ChicagoII", 
"California"), class = "data.frame", row.names = c("1500", "3000", 
"4500", "6000")) 

Dữ liệu:

West-Virginia ChicagoI ChicagoII California 
1500 29 30 39 2 
3000 29 21 42 8 
4500 23 36 35 6 
6000 23 26 46 5 
+0

thể bạn 'dput (autos_data)' – rbm

Trả lời

3

Bạn có thể tính toán các huyền thoại trực tiếp trong barplot cuộc gọi, quy định cụ thể đối số thông qua tham số args.legend và văn bản qua số legend.text. Đối với lưới, bạn cần phải thay thế barplot sau khi đặt lưới, với màu trắng và sau đó là các đường để lưới không xuất hiện nữa trong các dòng:

Cần 3 cuộc gọi giống nhau barplot nhưng tôi nghĩ nó mang lại bạn biết những gì bạn muốn:

# first plot 
barplot(t(as.matrix(autos_data)), main=NULL, axes=FALSE, beside=TRUE, col="white", ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50)) 
# add the grid 
grid(NA, 5, lwd=1,lty=5, col="gray") # grid only in y-direction 
# hide the grid with white bars 
barplot(t(as.matrix(autos_data)), main=NULL, beside=TRUE, col="white", ylab= "% of Cloud usage", xlab="Input data size (MB)", ylim=c(0,50), add=TRUE) 
# plot the density lines, the legend, etc. 
barplot(t(as.matrix(autos_data)), main=NULL, axes=FALSE, beside=TRUE, 
     lwd=1:2, angle=c(45,135), density=seq(5,40,10), col=c("blue","red","black","darkgreen"), 
     legend.text=c("WestVirginia","Chicago I","Chicago II","California"), 
     args.legend=list(x="top", y=NULL, bty="n", ncol=4), add=TRUE) 

enter image description here

4

Đây là một mâu thuẫn của tên tham số.

Bạn cần phải thiết lập fill:

legend(coords, c("WestVirginia","Chicago I","Chicago II","California"), text.col=par("col"), 
     cex=1.2, bty="n", xpd=TRUE, horiz=TRUE, inset=c(0,-.22), angle=c(35,135,45,135), 
     density=seq(5,40,10), fill=c("blue","red","black","darkgreen")) 

enter image description here

+0

Cleaner hơn câu trả lời của tôi (bây giờ bị xóa) –

0

Trong legend của bạn thiết fill hơn col

legend(
    coords, 
    legend = c("WestVirginia","Chicago I","Chicago II","California"), 
    text.col=par("col"), 
    cex=1.2, 
    bty="n", 
    xpd=TRUE, 
    horiz=TRUE, 
    inset=c(0,-.22), 
    angle=c(35,135,45,135), 
    density=seq(5,40,10), 
    fill=c("blue","red","black","darkgreen") 
) 
Các vấn đề liên quan