将图像放置在R中的美国Map上-接收强制失败

xu3bshqb  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(105)

当前代码:

library(USAboundaries)
#install.packages("USAboundariesData", repos = "https://ropensci.r-universe.dev", type = "source")
    library(sf)
    #> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
    library(tidyverse)
    library(rasterpic)
    library(tidyterra)
    
    states <- USAboundaries::states_contemporary_lores %>%
      select(State = state_abbr) %>%
      filter(!(State %in% c("AK", "HI", "PR"))) %>%
      st_transform("ESRI:102003") 
    
    states

    plot <- ggplot(states) +
      geom_sf(fill = "white") +
      theme_minimal() +
      theme(panel.background = element_rect(fill = "lightblue1"))
    
    plot
   
    teams <- data.frame(State = January$ShipState)
    teams$Team <- data.frame(Team = January$Team)
   
    logos <- teams %>%
      rowwise() %>%
      mutate(png = switch(Team$Team,
                          "Kansas City Chiefs" = "https://cdn.freebiesupply.com/images/large/2x/kansas-city-chiefs-logo-transparent.png",
                          "Alabama Crimson Tide" = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/Alabama_Crimson_Tide_logo.svg/1024px-Alabama_Crimson_Tide_logo.svg.png",
                          "Phoenix Suns" = "https://banner2.cleanpng.com/20180419/lpe/kisspng-phoenix-suns-nba-sacramento-kings-charlotte-hornet-basketball-team-logo-5ad88205c602d4.1820850415241385018111.jpg",
                          "San Francisco 49ers" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b55.png", 
                          "Colorado Avalanche" = "http://assets.stickpng.com/images/5a4fbb29da2b4f099b95da0b.png", 
                          "Tampa Bay Buccaneers" = "http://assets.stickpng.com/thumbs/580b585b2edbce24c47b2b5e.png", 
                          "Washington Capitals" = "http://assets.stickpng.com/images/5a4fbbeada2b4f099b95da22.png",
                          "Philadelphia Eagles" = "http://assets.stickpng.com/thumbs/580b585b2edbce24c47b2b4b.png",
                          "Georgia Bulldogs" = "https://flyclipart.com/thumb2/logo-university-of-georgia-bulldogs-bulldog-head-585520.png",
                          "Chicago Bears" = "https://cdn.freebiesupply.com/images/large/2x/chicago-bears-logo-transparent.png", 
                          "Cincinnati Bengals" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b19.png", 
                          "New Orleans Saints" = "https://cdn.freebiesupply.com/images/large/2x/new-orleans-saints-logo-transparent.png",
                          "Baltimore Ravens" = "https://cdn.freebiesupply.com/images/large/2x/baltimore-ravens-logo-transparent.png",
                          "Minnesota Wild" = "http://assets.stickpng.com/images/5a4fbb61da2b4f099b95da12.png",
                          "Vegas Golden Knights" = "http://assets.stickpng.com/images/5a4fbbe1da2b4f099b95da21.png",
                          "Buffalo Bills" = "https://cdn.freebiesupply.com/images/large/2x/buffalo-bills-logo-transparent.png",
                          "Pittsburgh Steelers" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b4e.png",
                          "Tennessee Titans" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b61.png",
                          "Dallas Cowboys"= 'https://cdn.freebiesupply.com/images/large/2x/dallas-cowboys-logo-transparent.png',
                          "Seattle Seahawks" = "https://cdn.freebiesupply.com/images/large/2x/seattle-seahawks-logo-transparent.png",
                          "Green Bay Packers" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b25.png",
                          "West Virginia Mountaineers" = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/West_Virginia_Mountaineers_logo.svg/1987px-West_Virginia_Mountaineers_logo.svg.png",
                          "Arkansas Razorbacks" = "https://upload.wikimedia.org/wikipedia/commons/e/ef/Arkansas-Razorback-Logo-2001.png",
                          "La Rams" = "http://assets.stickpng.com/images/608968b9d598a5000448eaa2.png",
                          "Washington Commanders" = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Washington_Commanders_logo.svg/2560px-Washington_Commanders_logo.svg.png",
                          "Philadelphia 76ers" = "http://assets.stickpng.com/images/58419ca3a6515b1e0ad75a64.png",
                          "Charlotte Fc" = "https://upload.wikimedia.org/wikipedia/en/thumb/9/91/Charlotte_FC_logo.svg/1200px-Charlotte_FC_logo.svg.png",
                          "Nebraska Cornhuskers" = "https://cdn.freebiesupply.com/logos/large/2x/nebraska-corn-huskers-logo-png-transparent.png",
                          "New York Yankees" = "http://assets.stickpng.com/images/584d4b740a44bd1070d5d494.png",
                          "Oklahoma Sooners" = "https://upload.wikimedia.org/wikipedia/commons/4/49/Oklahoma_Sooners_logo.png",
                          "Milwaukee Brewers" = "https://purepng.com/public/uploads/large/milwaukee-brewers-logo-9xg.png",
                          "Los Angeles Dodgers" = "http://assets.stickpng.com/images/584d3e8a0a44bd1070d5d3d7.png",
                          "Denver Broncos" = "https://cdn.freebiesupply.com/images/large/2x/denver-broncos-logo-transparent.png",
                          "Washington Nationals" = "https://cdn.freebiesupply.com/images/large/2x/washington-nationals-logo-transparent.png",
                          "Philadelphia Phillies" = "http://assets.stickpng.com/images/584d42ac0a44bd1070d5d420.png",
                          "Atlanta Braves" = "https://1000logos.net/wp-content/uploads/2017/08/Atlanta-Braves-logo.png",
                          "Iowa Hawkeyes" = "https://1000logos.net/wp-content/uploads/2021/06/Iowa-Hawkeyes-logo.png",
                          "Gonzaga University Bulldogs" = "https://www.pngkey.com/png/full/65-655460_bulldog-700x441-gonzaga-bulldog.png",
                          "Chicago Cubs" = "http://assets.stickpng.com/images/584d41b10a44bd1070d5d411.png",
                          "Kentucky Wildcats" = "https://upload.wikimedia.org/wikipedia/commons/5/5d/Kentucky_Wildcats_logo_2015.png",
                          "Lsu Tigers" = "https://www.nicepng.com/png/full/96-961386_lsu-football-png-transparent-lsu-football-louisiana-state.png",
                          "Boston Red Sox"= "https://cdn.freebiesupply.com/images/large/2x/boston-red-sox-logo-transparent.png",
                          "Detroit Tigers" = "https://cdn.freebiesupply.com/images/large/2x/detroit-tigers-logo-transparent.png",
                          "Minnesota Twins" = "http://assets.stickpng.com/images/584d47ae0a44bd1070d5d45d.png",
                          "St Louis Cardinals" = "http://assets.stickpng.com/images/584d3fc80a44bd1070d5d3ed.png",
                          "Mississippi State Bulldogs" = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Mississippi_State_Bulldogs_logo.svg/640px-Mississippi_State_Bulldogs_logo.svg.png",
                          "Cleveland Browns" = "https://cdn.freebiesupply.com/images/large/2x/cleveland-browns-logo-transparent.png",
                          "Portland Trailblazers" = "https://cdn.freebiesupply.com/images/large/2x/portland-trail-blazers-logo-transparent.png",
                          "Masters" = "https://logos-world.net/wp-content/uploads/2022/02/Masters-Symbol.png",
                          "Tennessee Vols" = "https://1000logos.net/wp-content/uploads/2021/07/Tennessee-Volunteers-logo.png",
                          "Houston Astros"= "http://assets.stickpng.com/images/584d493b0a44bd1070d5d477.png",
                          "Utah Jazz" = "http://assets.stickpng.com/images/58419bb6a6515b1e0ad75a55.png",
                          "La Lakers" = "http://assets.stickpng.com/images/58419d0aa6515b1e0ad75a6c.png",
                          "Kansas Jayhawks" = "https://brandslogos.com/wp-content/uploads/images/large/kansas-jayhawks-logo.png",
                          "Baltimore Orioles" = "https://upload.wikimedia.org/wikipedia/en/thumb/7/75/Baltimore_Orioles_cap.svg/1200px-Baltimore_Orioles_cap.svg.png",
                          "Unc Tar Heels" = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/North_Carolina_Tar_Heels_logo.svg/512px-North_Carolina_Tar_Heels_logo.svg.png",
                          "South Carolina Gamecocks" = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/South_Carolina_Gamecocks_logo.svg/933px-South_Carolina_Gamecocks_logo.svg.png",
                          "Seattle Mariners" = "http://assets.stickpng.com/images/584d467c0a44bd1070d5d44c.png",
                          "Colorado Rockies" = "https://www.nicepng.com/png/full/141-1415329_colorado-rockies-logos-png-clipart-download-colorado-rockies.png",
                          "Golden State Warriors" = "http://assets.stickpng.com/images/58419ce2a6515b1e0ad75a69.png",
                          "Chicago White Sox" = "http://assets.stickpng.com/images/584d4aff0a44bd1070d5d48d.png",
                          "Boston Celtics" = "http://assets.stickpng.com/images/58419c6aa6515b1e0ad75a61.png",
                          "Carolina Hurricanes" = "https://toppng.com/uploads/preview/carolina-hurricanes-nhl-logo-png-11536005105kxafnwdw2o.png",
                          "Tampa Bay Lightning" = "http://assets.stickpng.com/images/5a4fbbc8da2b4f099b95da1e.png",
                          "Oregon Ducks" = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Oregon_Ducks_logo.svg/928px-Oregon_Ducks_logo.svg.png",
                          "Milwaukee Bucks" = "http://assets.stickpng.com/images/58419ba7a6515b1e0ad75a54.png",
                          "Los Angeles Angels" = "http://assets.stickpng.com/images/584d498d0a44bd1070d5d47b.png",
                          "Carolina Panthers" = "https://cdn.freebiesupply.com/images/large/2x/carolina-panthers-logo-transparent.png",
                          "Minnesota Vikings" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b38.png",
                          "New England Patriots" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b3b.png",
                          "Clemson Tigers" = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/72/Clemson_Tigers_logo.svg/1071px-Clemson_Tigers_logo.svg.png",
                          "Wyoming Cowboys" = "https://cdn.freebiesupply.com/logos/large/2x/wyoming-cowboys-logo-png-transparent.png",
                          "Arizona Cardinals" = "https://cdn.freebiesupply.com/images/large/2x/arizona-cardinals-logo-transparent.png",
                          "Washington Football Team" = "http://assets.stickpng.com/images/60896cf3d598a5000448eaa8.png",
                          "Indianapolis Colts" = "http://assets.stickpng.com/images/580b585b2edbce24c47b2b2c.png",
                          "Detroit Lions" = "https://cdn.freebiesupply.com/images/large/2x/detroit-lions-logo-transparent.png",
                          "Las Vegas Raiders" = "https://www.pngmart.com/files/16/Las-Vegas-Raiders-Transparent-Background.png",
                          "Michigan State Spartans" = "http://assets.stickpng.com/images/6103b47a2d29570004bc56f2.png",
                          "Utah Utes" = "http://assets.stickpng.com/images/5859a2df4f6ae202fedf28ec.png",
                          "Seattle Kraken" = "https://www.gannett-cdn.com/presto/2020/07/23/PPAS/b671207b-9fd0-46ea-a78a-f1c0fd3cab7d-Edn6Cc3XgAALN8o.jpg",
                          "Michigan Wolverines" = "https://upload.wikimedia.org/wikipedia/commons/3/36/Michigan_Wolverines_Block_M.png"
      ))
    
    for (i in seq_len(nrow(logos))) {
      logo <- logos[i, ]
      shape <- states[states$State == logo$State, ]
      
      img <- rasterpic_img(shape, logo$png, mask = TRUE)
      
      plot <- plot + geom_spatraster_rgb(data = img)
    }
    
    plot

目前,我收到此错误:错误:[as,sf]强制失败。您可以尝试通过Spatial *(sp)类进行强制。
这样做的目的是在每个州都有一个带有体育标志的美国Map。这是每个州在谷歌上搜索最多的运动队。所以如果洋基队在纽约卖得最多,我希望洋基队的标志标在州的顶部。我使用的数据集非常非常简单。它是两列--州名和队名。(Ioidee. A列(ShipState)是AL,B列(Team)是阿拉巴马红潮。)由于某种原因,循环失败了。有人能告诉我可能出了什么问题吗?

h7appiyu

h7appiyu1#

你有没有试过在本地存储图像,而不是从互联网上获取?问题是,如果任何网站宕机,图像就不会出现。
此外,嵌入图像源代码是必需的。例如:

<img src="https://s1.pngtank.com/thumbnails/63c1c0b584396.png">

相关问题