日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

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

發布時間:2025/3/20 HTML 46 豆豆

在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浏览器中可以正常打印呢? - 堆栈内存溢出...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。