R:ggplot2绘制带有错误消息和警告消息的地图

时间:2020-04-02 23:57:22

标签: r ggplot2

我正在尝试使用ggplot2可视化地图上的数据

我按照以下网站上的说明进行操作:https://socviz.co/maps.html。我以枪支自杀为例。

代码如下:

\documentclass{article}
\usepackage{spverbatim}

\begin{document}

\begin{spverbatim}
!AIVDM,1,1,,A,13`gcphP000FeiVMuT3>4?wD20S1,0*1D
!AIVDM,1,1,,A,33MIo650000FFE>Mw:V00:iB0DSJ,0*1F
!AIVDM,1,1,,B,13`lS<@P00PFPwLMtUJN4?wD28Ht,0*4F
!AIVDM,1,1,,B,13aJIGwP0cPFMtNMv@k7rgwF2@I5,0*1A
!AIVDM,1,1,,A,13aI9rUP19PFvwLMrLb@NgwF28I6,0*2D
\end{spverbatim}

\end{document}

我的数据如下:

cancer$rate_cut=cut(cancer$rate,c(5,10,15,20,25,30))

county_map %>% sample_n(5)

orange_pal <- RColorBrewer::brewer.pal(n = 6, name = "Oranges")
orange_pal
## [1] "#FEEDDE" "#FDD0A2" "#FDAE6B" "#FD8D3C" "#E6550D"
## [6] "#A63603"
orange_rev <- rev(orange_pal)
orange_rev
## [1] "#A63603" "#E6550D" "#FD8D3C" "#FDAE6B" "#FDD0A2"
## [6] "#FEEDDE"

can_p <- ggplot(data = county_full,
                mapping = aes(x = long, y = lat,
                              fill = cancer$rate_cut, 
                              group = group))

can_p1 <- can_p + geom_polygon(color = "gray90", size = 0.05) + coord_equal()

can_p2 <- can_p1 + scale_fill_manual(values = orange_pal)

can_p2 + labs(title = "Gun-Related Suicides, 1999-2015",
              fill = "Rate per 100,000 pop.") +
  theme_map() + theme(legend.position = "bottom")

虽然这里全都是NA,但我确实有以下数据。

运行代码时,R给了我一条错误消息:

head(county_full)
     long      lat order  hole piece            group    id county_name rate
1 1225889 -1275020     1 FALSE     1 0500000US01001.1 01001        <NA>   NA
2 1235324 -1274008     2 FALSE     1 0500000US01001.1 01001        <NA>   NA
3 1244873 -1272331     3 FALSE     1 0500000US01001.1 01001        <NA>   NA
4 1244129 -1267515     4 FALSE     1 0500000US01001.1 01001        <NA>   NA
5 1272010 -1262889     5 FALSE     1 0500000US01001.1 01001        <NA>   NA
6 1276797 -1295514     6 FALSE     1 0500000US01001.1 01001        <NA>   NA
  count population rate_cut
1    NA         NA     <NA>
2    NA         NA     <NA>
3    NA         NA     <NA>
4    NA         NA     <NA>
5    NA         NA     <NA>
6    NA         NA     <NA>

================================================ =========

感谢您的评论,我通过加入数据(cancer和county_map)修改了代码。我得到了地图,但也收到了警告消息。

最新代码如下:

Error: Aesthetics must be either length 1 or the same as the data (191382): fill
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
Aesthetics must be either length 1 or the same as the data (191382): fill
Backtrace:
 1. (function (x, ...) ...
 2. ggplot2:::print.ggplot(x)
 4. ggplot2:::ggplot_build.ggplot(x)
 5. ggplot2:::by_layer(function(l, d) l$compute_aesthetics(d, plot))
 6. ggplot2:::f(l = layers[[i]], d = data[[i]])
 7. l$compute_aesthetics(d, plot)
 8. ggplot2:::f(..., self = self)
 9. ggplot2:::check_aesthetics(evaled, n)

0 个答案:

没有答案