ggplot2多边形->错误:提供给连续比例的离散值

Posted

技术标签:

【中文标题】ggplot2多边形->错误:提供给连续比例的离散值【英文标题】:ggplot2 polygon -> Error: Discrete value supplied to continuous scale 【发布时间】:2014-05-27 15:18:39 【问题描述】:

我正在尝试绘制几个国家并根据某些变量用颜色填充它们,并且我已经为一个国家实现了它,但是当我尝试将县相乘时,我不断收到错误:提供连续规模的离散值虽然代码基本相同,所以这里是非工作代码及其数据集

library(ggplot2)
library(maptools)
library(rgeos)
library(rgdal)
library(gpclib)
library(dplyr)

mne <- get(load('/Users/praeconium/Desktop/Dev/NonResponse/GoliOtok/Paper/Shapes/MNE_adm0.RData'))
sr <- get(load('/Users/praeconium/Desktop/Dev/NonResponse/GoliOtok/Paper/Shapes/SRB_adm0.RData'))
sl <- get(load('/Users/praeconium/Desktop/Dev/NonResponse/GoliOtok/Paper/Shapes/SVN_adm0.RData'))
hr <- get(load('/Users/praeconium/Desktop/Dev/NonResponse/GoliOtok/Paper/Shapes/HRV_adm0.RData'))
bh <- get(load('/Users/praeconium/Desktop/Dev/NonResponse/GoliOtok/Paper/Shapes/BIH_adm0.RData'))
mk <- get(load('/Users/praeconium/Desktop/Dev/NonResponse/GoliOtok/Paper/Shapes/MKD_adm0.RData'))

gpclibPermit()

mne.adm0.df <- fortify(mne, region = "NAME_ENGLISH")
sr.adm0.df <- fortify(sr, region = "NAME_ENGLISH")
sl.adm0.df <- fortify(sl, region = "NAME_ENGLISH")
hr.adm0.df <- fortify(hr, region = "NAME_ENGLISH")
bh.adm0.df <- fortify(bh, region = "NAME_ENGLISH")
mk.adm0.df <- fortify(mk, region = "NAME_ENGLISH")

combine <- 0
combine <- rbind(combine, mne.adm0.df)
combine1 <- rbind(combine, sr.adm0.df)
combine2 <- rbind(combine1, sl.adm0.df)
combine3 <- rbind(combine2, hr.adm0.df)
combine4 <- rbind(combine3, bh.adm0.df)
combine5 <- rbind(combine4, mk.adm0.df)

rm(combine,combine1,combine2,combine3, combine4)

combine6 <- combine5[-1,]
States <- data.frame(id=unique(combine6$id), emp=c(1,2,3,1,2,3), long=c("19.09462","19.70397","16.28719","16.40274","16.96187","22.23574"), lat=c("43.52889","46.18837","46.8704","46.43516","45.22893","42.3041"))

Set <- merge(combine6, States, by='id')

p <- ggplot(Set, aes(x = lat.x, y = long.x, group = group)) +
geom_polygon(aes(fill = cut(emp,10))) +
geom_text(data = States, aes(label = id, x = long, y = lat, group = id), size = 3) +
labs(x=" ", y=" ") + 
theme_bw() + scale_fill_brewer('Rate', palette  = 'PuRd') + 
coord_map() + 
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) + 
theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) + 
theme(panel.border = element_blank())
print(p)

会话输出和数据集

> str(Set)
'data.frame':   220148 obs. of  10 variables:
 $ id    : chr  "Bosnia and Herzegovina" "Bosnia and Herzegovina" "Bosnia and Herzegovina" "Bosnia and Herzegovina" ...
$ long.x: num  16.9 16.9 16.9 16.9 17 ...
$ lat.x : num  45.2 45.2 45.2 45.2 45.2 ...
$ order : num  1 2 3 4 5 6 7 8 9 10 ...
$ hole  : num  0 0 0 0 0 0 0 0 0 0 ...
$ piece : Factor w/ 521 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ group : Factor w/ 531 levels "Montenegro.1",..: 530 530 530 530 530 530 530 530 530 530 ...
$ emp   : num  2 2 2 2 2 2 2 2 2 2 ...
$ long.y: Factor w/ 6 levels "16.28719","16.40274",..: 3 3 3 3 3 3 3 3 3 3 ...
$ lat.y : Factor w/ 6 levels "42.3041","43.52889",..: 3 3 3 3 3 3 3 3 3 3 ...

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] psych_1.3.2     gridExtra_0.9.1 lubridate_1.3.3 mapproj_1.2-1   maps_2.3-6      dplyr_0.1       gpclib_1.5-5    rgdal_0.8-16   
[9] rgeos_0.3-4     maptools_0.8-29 sp_1.0-14       ggplot2_0.9.3.1

loaded via a namespace (and not attached):
[1] assertthat_0.1     colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       foreign_0.8-55     gtable_0.1.2      
[7] labeling_0.2       lattice_0.20-23    MASS_7.3-29        memoise_0.1        munsell_0.4.2      plyr_1.8          
[13] proto_0.3-10       RColorBrewer_1.0-5 Rcpp_0.11.0        reshape2_1.2.2     scales_0.2.3       stringr_0.6.2     
[19] tools_3.0.2       

> headTail(Set)
                       id long.x lat.x order hole piece                    group emp   long.y    lat.y
1      Bosnia and Herzegovina  16.93 45.23     1    0     1 Bosnia and Herzegovina.1   2 16.96187 45.22893
2      Bosnia and Herzegovina  16.94 45.23     2    0     1 Bosnia and Herzegovina.1   2 16.96187 45.22893
3      Bosnia and Herzegovina  16.95 45.23     3    0     1 Bosnia and Herzegovina.1   2 16.96187 45.22893
4      Bosnia and Herzegovina  16.95 45.23     4    0     1 Bosnia and Herzegovina.1   2 16.96187 45.22893
...                      <NA>    ...   ...   ...  ...  <NA>                     <NA> ...     <NA>     <NA>
220145               Slovenia   13.6 45.49  2791    0     2               Slovenia.2   3 16.28719  46.8704
220146               Slovenia   13.6 45.49  2792    0     2               Slovenia.2   3 16.28719  46.8704
220147               Slovenia   13.6  45.5  2793    0     2               Slovenia.2   3 16.28719  46.8704
220148               Slovenia   13.6  45.5  2794    0     2               Slovenia.2   3 16.28719  46.8704

这是工作代码及其输出,基本相同,只是文件位于不同的管理级别

library(ggplot2)
library(maptools)
library(rgeos)
library(rgdal)
library(gpclib)
load("/Users/praeconium/Downloads/MNE_adm1.RData")
mne.adm2.spdf <- get("gadm")

gpclibPermit()
mne.adm2.df <- fortify(mne.adm2.spdf, region = "NAME_1")

id <- unique(mne.adm2.df$id)
emp <- c(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3)
unemp <- data.frame(id,emp)

mne2 <- merge(mne.adm2.df, unemp, by='id')
mne.centro <- data.frame(long = coordinates(mne.adm2.spdf)[, 1], lat = coordinates(mne.adm2.spdf)[, 2])
towns <- c("And", "Bar", "Berane", "BP", "BD", "CT", "DG", "HN", "KOl", "KO", "MK", "NK", "PL", "PV", "PLu", "PG", "RO", "SA", "TV", "UL", "ZB")
id <- unique(mne.adm2.df$id)
mne.centro3 <- data.frame(towns, mne.centro, id)


# Plot
p <- ggplot(mne2, aes(x = long, y = lat, group = group)) + 
geom_polygon(aes(fill = cut(emp,5))) +
geom_text(data = mne.centro3, aes(label = towns, x = long, y = lat, group = towns), size = 3) + 
labs(x=" ", y=" ") + 
theme_bw() + scale_fill_brewer('Rate', palette  = 'PuRd') + 
coord_map() + 
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) + 
theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) + 
theme(panel.border = element_blank())
print(p)

 > str(mne2)
   data.frame': 12931 obs. of  8 variables:
  $ id   : chr  "Andrijevica" "Andrijevica" "Andrijevica" "Andrijevica" ...
  $ long : num  19.9 19.9 19.9 19.8 19.8 ...
  $ lat  : num  42.7 42.7 42.7 42.7 42.7 ...
  $ order: int  1 2 3 4 5 6 7 8 9 10 ...
  $ hole : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
  $ piece: Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
  $ group: Factor w/ 27 levels "Andrijevica.1",..: 1 1 1 1 1 1 1 1 1 1 ...
  $ emp  : num  1 1 1 1 1 1 1 1 1 1 ...

 > sessionInfo()
 R version 3.0.2 (2013-09-25)
 Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils    
[6] datasets  methods   base     

other attached packages:
[1] psych_1.3.2     gridExtra_0.9.1 lubridate_1.3.3
[4] mapproj_1.2-1   maps_2.3-6      dplyr_0.1      
[7] gpclib_1.5-5    rgdal_0.8-16    rgeos_0.3-4    
[10] maptools_0.8-29 sp_1.0-14       ggplot2_0.9.3.1

loaded via a namespace (and not attached):
[1] assertthat_0.1     colorspace_1.2-2  
[3] dichromat_2.0-0    digest_0.6.3      
[5] foreign_0.8-55     gtable_0.1.2      
[7] labeling_0.2       lattice_0.20-23   
[9] MASS_7.3-29        memoise_0.1       
[11] munsell_0.4.2      plyr_1.8          
[13] proto_0.3-10       RColorBrewer_1.0-5
[15] Rcpp_0.11.0        reshape2_1.2.2    
[17] scales_0.2.3       stringr_0.6.2     
[19] tools_3.0.2       

> headTail(mne2)
           id  long   lat order  hole piece         group emp
1     Andrijevica 19.89 42.68     1 FALSE     1 Andrijevica.1   1
2     Andrijevica 19.88 42.67     2 FALSE     1 Andrijevica.1   1
3     Andrijevica 19.85 42.66     3 FALSE     1 Andrijevica.1   1
4     Andrijevica 19.84 42.66     4 FALSE     1 Andrijevica.1   1
...          <NA>   ...   ...   ...  <NA>  <NA>          <NA> ...
12928     Žabljak 19.02 43.25 12928 FALSE     1     Žabljak.1   3
12929     Žabljak 19.02 43.26 12929 FALSE     1     Žabljak.1   3
12930     Žabljak 19.03 43.26 12930 FALSE     1     Žabljak.1   3
12931     Žabljak 19.03 43.26 12931 FALSE     1     Žabljak.1   3

我已经按照这些问题解决了这些问题,但我仍然不明白我做错了什么? Administrative regions map of a country with ggmap and ggplot2 我检查了这些和这些 Specifying the colour scale for maps in ggplot Add color to boxplot - "Continuous value supplied to discrete scale" error

【问题讨论】:

好吧,scale_fill_brewer(...) 创建离散值。在您的底部示例中,fill=cut(emp,5) 似乎也是离散的,而在您的顶部示例中,fill=cut(emp,10) 是连续的。如果没有看到您的完整数据集,我无法说出原因。 Set$emp 中的值是什么? tl;dr,请提供一个可重现的小例子。 与其在 geom_polygon 调用中即时计算您的cut,不如将​​其作为列添加到您的数据中。这样您就可以看到它的输出并确保它是一个因素,以便您知道您将离散变量赋予离散尺度。 好的,谢谢,但请在下面查看我的答案。似乎 geom_text 是原因。 【参考方案1】:

根据我自己有限的经验,我发现我通常需要分解我的分组条件。如果您的数据被解释为连续的,您可以通过分解将其强制为离散的。

我最关心的变量是mne2$empmne2$id,尤其是后者,因为在这种情况下,变量towns 很可能会被视为离散变量,而mne2$id 很可能会被视为连续变量。 group = factor(mne2$id) 的效果可以解决您的问题。

【讨论】:

【参考方案2】:

我通过排除部分解决了目标,但没有解决手头的任务

geom_text(data = mne.centro3, aes(label = towns, x = long, y = lat, group = towns), size = 3) + 

但我不明白相同的代码在这里是连续的,而那里是离散的:S

【讨论】:

以上是关于ggplot2多边形->错误:提供给连续比例的离散值的主要内容,如果未能解决你的问题,请参考以下文章

默认数据集示例 mtcars 和 ggplot2 中的“错误:提供给离散比例的连续值”

提供给离散比例ggplot2的连续值

向箱线图添加颜色 - “提供给离散比例的连续值”错误

ggplot2:方面的连续和离散比例

即使我使用数字,ggplot2中的离散值/连续比例误差

查找比例 x 或 y 连续的限制或避免删除 ggplot2 和 ggmap 中的行