2016-01-13 59 views
0

enter image description here如何根據密度繪圖

我想做一個空間圖。我做了情節。我用不同的顏色來識別不同的值。但是,我想要一種顏色的不同色調。是否有任何方法根據較高到較低的值使用相同顏色的不同陰影繪製同一個陰謀?

library(maps) 

C <-88 
A <- c(6.79,10.64,11.06,11.59,2.46,10.64,3.22,6.79,6.79, 
    11.06, 4.82,4.82,6.79,6.79,11.06,3.22,8.12,11.59, 
    4.82,10.64,8.12,10.64,8.12,8.12,8.12,10.64,2.46, 
    11.59,4.82,3.22,6.79,10.64,8.12,3.22,10.64,6.79, 
    2.46,11.06,10.64,2.46,3.22,8.12,11.59,2.46,8.12, 
    8.12,11.59,10.64,8.12,11.06,8.12,11.06,2.46,10.64, 
    4.82,3.22,4.82,3.22,8.12,3.22,3.22,10.64,10.64,2.46, 
    8.12,2.46,11.06,4.82,10.64,11.06,2.46,10.64,2.46, 
    10.64,4.82,11.06,11.06,11.06, 11.06,8.12, 10.64,2.46, 
    6.79, 3.22, 11.06, 10.64,10.64,8.12) 

A.color <- array(NA,C) 
for (i in 1:C) { 
    if(A[i] == 10.64) A.color[i] <- c("red") 
    if(A[i] == 11.59) A.color[i] <- c("blue") 
    if(A[i] == 4.82) A.color[i] <- c("green") 
    if(A[i] == 8.12) A.color[i] <- c("purple") 
    if(A[i] == 11.06) A.color[i] <- c("magenta") 
    if(A[i] == 6.79) A.color[i] <- c("black") 
    if(A[i] == 2.46) A.color[i] <- c("yellow") 
    if(A[i] == 3.22) A.color[i] <- c("violet") 
} 

map("county", "ohio", fill = TRUE, col=A.color[], 
border=c(0,0),mar=c(0,0,0,0),main="Undertriage") 
title("Undertriage plot by region") 

legend("bottomright",legend=c("UT-18.12 %","UT-19.75 %", "UT-8.21 %","UT- 13.84 %","UT-18.85 %", "UT-11.56 %", "UT-4.20 %","UT-5.48 %"), 

fill=c("red","blue","green","purple","magenta","black","yellow","violet"), 
[![enter image description here][1]][1]bty="n", cex=1.0, horiz=F) 

回答

3

您可以隨時使用rgb()生成顏色:

A.color <- rgb(1, 0, 0, alpha = (A + 0.5)/(max(A) + 0.5)) 

map("county", "ohio", fill = TRUE, col=A.color, 
    border=c(0,0),mar=c(0,0,0,0),main="Undertriage") 
title("Undertriage plot by region") 

enter image description here

A.color <- rgb(1.2*max(A) - A, 0, 0, maxColorValue = max(A)) 

map("county", "ohio", fill = TRUE, col=A.color, 
    border=c(0,0),mar=c(0,0,0,0),main="Undertriage") 
title("Undertriage plot by region") 

enter image description here

或者,也許看看?heat.colors

A.color <- heat.colors(length(unique(A))) 
A.color <- A.color[c(factor(A))] 

map("county", "ohio", fill = TRUE, col=A.color, 
    border=c(0,0),mar=c(0,0,0,0),main="Undertriage") 
title("Undertriage plot by region") 

enter image description here

+0

非常感謝。但我想要不同程度的相同顏色,隨着密度的降低,這些顏色將呈現深紅色以獲得更高的密度,並顯示更淺的紅色。它會顯示一側顯示顏色分佈的欄。 – Benzamin

+0

你能告訴我如何在這裏添加圖例嗎? @ J.R – Benzamin

+0

是的,如果您提供有關傳說與「A」相關的數據,這不是問題,因此我們不需要像您那樣手動執行。然後我們首先對圖例和顏色進行排序。 –