gpt4 book ai didi

r - ggplot2 多边形 -> 错误 : Discrete value supplied to continuous scale

转载 作者:行者123 更新时间:2023-12-02 03:36:37 25 4
gpt4 key购买 nike

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

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)

session 输出和数据集

> 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

working one

为了解决这些问题,我已经遵循这些问题,但我仍然不明白我做错了什么? 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

最佳答案

我通过排除部分解决了目标但不是手头的任务

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

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

关于r - ggplot2 多边形 -> 错误 : Discrete value supplied to continuous scale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23046963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com