2017-03-24 95 views
3

我的問題與this one類似,只是我的數據不同。就我而言,我無法使用給出的解決方案。我希望點根據cut()值在我的地圖上顯示。有人能指出我正確的方向嗎?in R,ggplot geom_point()基於特定離散值的顏色 - 第2部分

> test 
# A tibble: 10 × 5 
     TC1 TC2  Lat  Long  Country 
     <dbl> <dbl>  <dbl>  <dbl>  <fctr> 
1  2.9 2678.0 50.62980 -95.60953  Canada 
2 1775.7 5639.9 -31.81889 123.19389  Australia 
3  4.4 5685.6 -10.10449 38.54364  Tanzania 
4  7.9  NA 54.81822 -99.91685  Canada 
5  11.2 2443.0 7.71667 -7.91667 Cote d'Ivoire 
6  112.1 4233.4 -17.35093 128.02609  Australia 
7  4.4 114.6 45.21361 -67.31583  Canada 
8 8303.5 4499.9 46.63626 -81.39866  Canada 
9 100334.8 2404.5 46.67291 -93.11937   USA 
10  NA 1422.9 -17.32921 31.28224  Zimbabwe 

ggplot(data = test, aes(x= Long, y= Lat)) + 
    borders("world", fill="gray75", colour="gray75", ylim = c(-60, 60)) + 
    geom_point(aes(size=TC2, col=cut(TC1, c(-Inf, 1000, 5000, 50000, Inf)))) + 
    # scale_colour_gradient(limits=c(100, 1000000), low="yellow", high="red") + 
    scale_color_manual(name = "TC1", 
        values = c("(-Inf,1000]" = "green", 
           "(1000,5000]" = "yellow", 
           "(5000,50000]" = "orange", 
           "(50000, Inf]" = "red"), 
        labels = c("up to 1", "1 to 5", "5 to 50", "greater than 50")) + 
    theme(legend.position = "right") + 
    coord_quickmap() 
Warning message: 
Removed 10 rows containing missing values (geom_point). 

enter image description here

回答

3

你幾乎沒有!這只是不正確的'切'因素的名稱。如果您嘗試:

cut(test$TC1, c(-Inf, 1000, 5000, 50000, Inf)) 
# [1] (-Inf,1e+03] (1e+03,5e+03] (-Inf,1e+03] (-Inf,1e+03] (-Inf,1e+03] 
# [6] (-Inf,1e+03] (-Inf,1e+03] (5e+03,5e+04] (5e+04, Inf] <NA>   
# Levels: (-Inf,1e+03] (1e+03,5e+03] (5e+03,5e+04] (5e+04, Inf] 

正如您所看到的級別名稱與您輸入的內容有點不同。

library(ggplot2) 

ggplot(data = test, aes(x = Long, y = Lat)) + 
    borders("world", fill="gray75", colour="gray75", ylim = c(-60, 60)) + 
    geom_point(aes(size=TC2, color = cut(TC1, c(-Inf, 1000, 5000, 50000, Inf)))) + 
    scale_color_manual(name = "TC1", 
    values = c("(-Inf,1e+03]" = "green", 
       "(1e+03,5e+03]" = "yellow", 
       "(5e+03,5e+04]" = "orange", 
       "(5e+04, Inf]" = "red"), 
    labels = c("up to 1", "1 to 5", "5 to 50", "greater than 50")) + 
    theme(legend.position = "right") + 
    coord_quickmap() 
#> Warning: Removed 2 rows containing missing values (geom_point). 

數據:

test <- read.table(text = 'TC1 TC2 Lat Long Country 
1  2.9 2678.0 50.62980 -95.60953  Canada 
2 1775.7 5639.9 -31.81889 123.19389  Australia 
3  4.4 5685.6 -10.10449 38.54364  Tanzania 
4  7.9  NA 54.81822 -99.91685  Canada 
5  11.2 2443.0 7.71667 -7.91667 "Cote d\'Ivoire" 
6  112.1 4233.4 -17.35093 128.02609  Australia 
7  4.4 114.6 45.21361 -67.31583  Canada 
8 8303.5 4499.9 46.63626 -81.39866  Canada 
9 100334.8 2404.5 46.67291 -93.11937   USA 
10  NA 1422.9 -17.32921 31.28224  Zimbabwe', header = T) 
+0

微妙!謝謝。兩個副業問題:是否有可能修改代碼以將NA顯示爲非彩色符號(例如黑色或灰色)......例如NA =「黑色」。什麼是可幫助您爲可重現實例吐出(部分)數據框的軟件包? – val

+1

1-是的,可以爲每個'scale_ *'添加一個特定的'na.value'。然而,雖然這可能適用於填充'aes',我建議不要使用它的大小。我不確定你指的是什麼。也許'dput(mydata)'?我不是一個軟件包,而是一個基本功能。用它來分享你在envir中的確切對象。 – GGamba