2017-02-06 53 views
1
ggplot(data = filter(My.Map, Year == 1435 & Some.Factor == 1), aes(x=long, y=lat, group = Group.Var, fill=as.numeric(Ageincrease))) + 
    geom_polygon() + 
    scale_fill_continuous(name="Age increase") + 
    geom_path(color = "white") + 
    coord_equal() + 
    coord_quickmap() 

不幸的是沒有示例的數據。我的數據包含一個變量 - Ageincrease,我想在地圖上填充爲contionous。如果是正面的,藍色,如果是負面的紅色。但也是漸變,邊上有2個色條。 Some.Factor是我創建的變量,用於指示Ageincrease是否爲負值/正值。GGPLOT2:取決於因子顏色長期最高變量也

回答

1

scale_colour_gradient2()默認情況下會爲紅色和藍色之間的連續變量生成漸變比例。將midpoint參數設置爲對您的數據有意義的內容(默認值爲0)。

library(ggplot2) 

ggplot(mtcars, aes(x=disp, y=mpg, col=hp)) + 
    geom_point(size = 5) + 
    scale_color_gradient2(midpoint = mean(mtcars$hp)) + 
    theme_bw() 

等價,scale_fill_gradient2()