在RStudio中,我使用tmap_leaflet函数制作了一张传单地图,其中根据变量的值对某些多边形进行了着色,并在多边形的顶部打印了一些粗线,这些变量将变量的值分为三类。 我希望它成为Rmd文件的一部分,该文件可以很好地编织,并且在RStudio弹出窗口中输出正确,但是在自动保存的html文件中缺少杂物。

这是用R studio编写的Rmd文件的可复制示例:

knitr::opts_chunk$set(echo = FALSE)

library(tigris) #to download a USA map

library(sf)

library(dplyr)

library(tmap) #to build the map

library(tmaptools) #for function append_data

library(ggplot2) #to produce the grobs

library(leaflet) # for setView function

#download the usa map shapefile as an sf file

us_geo

#keep only the 48 contiguous States of the US which will be the background of the map

us48_geo

"Hawaii",

"Puerto Rico",

"Commonwealth of the Northern Mariana Islands",

"United States Virgin Islands" ,

"Guam",

"American Samoa"

)) )

#data I want to plot on the map for four states

#the colour of the polygons of these States will depend on the value of Total_Number

#the grob for each state will be a pie chart showing the breakdown of the total number in the three categories, the percentages of which are also provided

dat_df

Total_Number = c(800, 1200, 1700, 2100),

Perc_Cat1 = c(30, 40, 50, 60),

Perc_Cat2 = c(10, 20, 30, 10),

Perc_Cat3 = c(60, 40, 20, 30))

#create an sf object for the four states I have data for

usStates_sf %

filter(STUSPS %in% c("WA", "CA", "FL", "NJ") ) %>%

append_data(dat_df, key.shp = "STUSPS", key.data = "STATE")

#to make grobs, data need to be in long format

dat_df_long

varying = list(3:5),

timevar = "Category",

v.names = "Perc",

idvar = "STATE",

direction="long")

#Category needs to be defined as factor so that scale_fill_manual() in ggplot() works as intended

dat_df_long$Category

#define the colours of the piechart

cat_cols

#make the grobs

grobs

dat_df_long$STATE), function(x) {

ggplotGrob( ggplot(x, aes(x="", y=-Perc, fill= Category)) +

geom_bar(width=1, stat="identity") +

scale_y_continuous(expand=c(0,0)) +

scale_fill_manual( values = cat_cols) +

theme_ps(plot.axes = FALSE) +

coord_polar("y", start=0) #to transform bar to pie

)

})

#to produce a static map

static_map

#background of the map:

tm_shape(us48_geo) + tm_polygons( col="white") +

#polygons of the four states I have data for coloured depending on the value of Total_Number

tm_shape(usStates_sf) +

tm_polygons(col="Total_Number",

palette = "YlOrBr",

title ="Total Number",

contrast = 0.7,

id = "STUSPS",

popup.vars = c("Total Number" = "Total_Number")) +

#add the grobs (piecharts) for each State

tm_symbols(shape="STUSPS",

shapes=grobs,

scale=1.5,

legend.shape.show = FALSE,

legend.size.is.portrait = TRUE,

shapes.legend = 22,

id = "STUSPS",

popup.vars = c("Category 1 (%)" ="Perc_Cat1",

"Category 2 (%)" = "Perc_Cat2",

"Category 3 (%)" = "Perc_Cat3") ) +

tm_add_legend(type="fill",

col=cat_cols,

labels= levels(dat_df_long$Category),

title="Category") +

tm_layout(legend.stack = "horizontal" )

#render the static map into a leaflet map

leafletmap % setView(-96, 37.8, zoom=4)

要绘制的地图如下

leafletmap

在RStudio中一切正常,但自动保存在我的工作文件夹中的html文件中缺少这些杂物。

r怎么保存html文件,leaflet - 为什么在已保存的html文件中缺少传单地图上的杂项,但在Rstudio浏览器中可以正常打印呢? - 堆栈内存溢出...相关推荐

  1. linux删除文件最后一行换行符,linux - 使用sed删除最后一个换行符 - 堆栈内存溢出...

    1)从文件中删除最后一个空行: 首先,您当前使用的命令将删除所有空行和空行! 不只是最后一个. 如果要删除最后一行为空/空白的行,则可以使用以下命令: sed '${/^[[:blank:]]*$/d ...

  2. pdf 中的java运行,java - 从pdf文件读取特定位置的itext在intellij中运行,并提供所需的输出,但是可执行jar抛出错误 - 堆栈内存溢出...

    我正在从n个页面的输入pdf文件中读取特定位置,并在这些位置上列出文本. 然后,我编写一个新的pdf文档,并将列表中的这些字符串写入包含单元格的表中. 我提出了两个主要问题. 我想在表中有三列,但是如 ...

  3. outlook删除服务器文件夹,如何还原已删除的Outlook文件夹[最佳方式]

    Outlook 是世界上最流行的电子邮件服务之一. 组织更有可能使用Outlook和Microsoft Exchange Online. Outlook的文件夹设计使电子邮件管理更容易. 用户可以创建 ...

  4. java获取excel文件第一行_java - 在Java中读取Excel文件,但第一行除外 - 堆栈内存溢出...

    我正在尝试从使用xlsx文件的JasperReport模板自动创建报告,我用来从excel文件读取的方法是这样的: String[] columnNames = new String[]{" ...

  5. 如何为linux 桌面文件内存,linux - 桌面Linux发行版中.desktop文件的功能是什么? - 堆栈内存溢出...

    我有以下情况:我必须修改一个.desktop文件,该文件位于我正在使用的应用程序的包中. 当我尝试打开文件的内容时,我遇到了一个奇怪的问题. 如果我点击它然后我尝试点击"打开"它给 ...

  6. android 删除路径文件内容,Android 删除已知路径的文件或文件夹

    转载:https://blog.csdn.net/qq_31939617/article/details/89414714 [功能] delete(String delFile):删除文件或文件夹 d ...

  7. android编译终止,android - Android NDK-没有此类文件或目录编译终止-头文件导出问题 - 堆栈内存溢出...

    我正在尝试使用NDK将TagLib C ++库( http://taglib.github.io/ )导入Android. 通过按如下所示将所有源代码放在/jni/taglibroot/下,我能够成功 ...

  8. java spring 打包后找不到路径,java - Spring中的类路径问题:找不到文件异常 - 堆栈内存溢出...

    我有一个简单的Java Spring IO项目,其中的一个类应该从csv文件读取,对于每个读取的记录,参数都存储在帐户对象列表中. 我正在使用Force IDE Luna,并且读取文件的Class C ...

  9. linux备份删除文件,linux - 在Linux中完成备份后编写脚本来删除备份文件 - 堆栈内存溢出...

    成功备份后,将创建日志文件为000000#.XLOG 和 我需要帮助的是如何动态搜索正确的备份日志文件(刚刚发生的备份),以便能够抓取文件并将其删除. 总之,我知道创建的最后一个.XLOG文件是与上一 ...

最新文章

  1. 图解SSL/TLS协议
  2. 密码学-hash散列表
  3. HDU- 1754 I Hate It
  4. python api测试框架_python api 测试框架
  5. Android 系统(135)---Android anr 分析步骤总结
  6. linux open dev/tty0 receive_buf,书写基于内核的linux键盘纪录器(p9-0e)(3)
  7. python代码示例-Python3 实例
  8. IP与子网掩码的计算
  9. RGB图像任意角度旋转
  10. AI加持,云之家V10让移动办公充满无限想象
  11. 报刊订阅管理系统的设计与实现
  12. bbs.php168,PHP168与PHPWIND深度联手 CMS+BBS整合将成趋势
  13. 阿里云-数据仓库-全链路大数据开发治理平台-DataWorks的数字世界
  14. 内存与IO,磁盘IO,网络IO
  15. 网页课程设计-期末大作业-简单设计【原神狂喜】
  16. 通信电子电路(一)通电课程背景 以及选频网络概念
  17. SQL Server 深入解析索引存储(上)
  18. 任务01-05-登录界面
  19. 爬虫字体替换(二)起点
  20. 联想 Lenovo 小新Pro-13ARE 2020 笔记本电脑 评测

热门文章

  1. MySQL事务的可串行化
  2. cnn卷积神经网络_卷积神经网络(CNN)原理及应用
  3. PPT怎么在剪映_剪映怎么剪辑音乐?添加、分割、导入音乐,图文详解!
  4. Spring 事务底层原理,你会了吗?
  5. hibernate教程--事务处理详解
  6. Oracle数据库之集合运算
  7. Oracle日期格式化问题:to_date(sysdate,'yyyy-MM-dd')与 to_date(to_char(sysdate,'yyyy-MM-dd'),'yyyy-MM-dd')区别
  8. python画画零基础_[零基础学python]画圈还不简单吗?
  9. python牛顿迭代法_python-来自维基百科示例的Gauss-Newton方法的实现
  10. 用户与订单之间的关系_wms与oms、tms的上下游关系