1

[US state with geom_path][1][US state with geom_point][2]I am using R to overlay US states shape file above ogallala region shape file. I would ideally like to have shape boundaries as line but I get poorly formed map when I try that (in pictures) but when I try geom_point it works alright. Can someone please explain what I am doing wrong. [US state with geom_line][3]


OG_HUC = read.csv("input/Ogallala_huc.csv")
OG_table =right_join(HUC8_map.df,OG_HUC,by = c("HUC_CODE"="HUC8"))
#OG_table = merge(HUC8_map.df,OG_HUC,by = "HUC8", sort = FALSE)
OG_table[is.na(OG_table)] = 0
#write.csv(OG_table,'OG_table.csv')


State <- readOGR( 
  dsn= paste0(getwd(),"/input/State") , 
  layer="states"
)
State_map <- spTransform(State, CRS("+proj=longlat +datum=WGS84"))
State_map@data$id = rownames(State_map@data)
State_map.points = fortify(State_map, region="id")

centroids.df <- as.data.frame(coordinates(State_map))
names(centroids.df) <- c("Longitude", "Latitude")  #more sensible column names

State_map.df1 = merge(State_map.points, State_map@data, by="id")
State_map.df2 = data.frame(id = State_map@data$id, State_map@data, centroids.df)




ggplot()+geom_polygon(data=OG_table,aes(x = long, y = lat, group=group),fill="lightskyblue",col="black", alpha = 0.3) +
  geom_text(data = State_map.df2, aes(Longitude, Latitude, label=STATE_ABBR),col="black")+
  #geom_path(data = OG_table, aes(long, lat, group=group),color="black") +
  geom_point(data = State_map.df1, aes(long, lat, label=STATE_ABBR),color="black")+
  coord_map(xlim = c(-108,-95),ylim = c(31,45))+
  scale_fill_identity()
```enter image description here 


  [1]: https://i.stack.imgur.com/7XzeB.png
  [2]: https://i.stack.imgur.com/9tAB8.png
  [3]: https://i.stack.imgur.com/MtgXs.png
Konica
  • 11
  • 1

1 Answers1

0

Try:

Library (maps) 

Your_data %>% ggplot (aes(lat, lon)) + 
                    Borders ("states") +
                    geom_polygon() 
Dave2e
  • 22,192
  • 18
  • 42
  • 50
The Elif
  • 39
  • 1
  • 9
  • 1
    R is case sensitive. It has a function `library`, not`Library`. And, `maps` do not have the `Borders` function. – yarnabrina May 26 '20 at 02:26