0

I have one CSV file and one shapefile

mydata <-readr::read_csv("~/map/done.csv")
mymap <- shapefile("~/map/tyumen.shp")

How merge it to one table and draw plot?

map_and_data <- merge(mydata, mymap, by='Country')


ggplot(map_and_data) +
  geom_sf(aes(fill=MeDianIncome))

ggplot(map_and_data)+ 
  geom_sf(aes(fill = MedianIncome))+
  scale_fill_gradient(low= "#56B1F7", high = "#132B43")

enter image description here

enter image description here

1

How about using *_join from dplyr package, for instance:

map_and_data <- dplyr::left_join(mymap, mydata, by='Country')

Here is one example you can test.

library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
my_table = data.frame(NAME= nc$NAME, INCOME= runif(100, 5000, 10000))

library(dplyr)
merged = left_join(nc, my_table, by= "NAME")

library(ggplot2)
ggplot(merged)+
    geom_sf(aes(fill= INCOME))+
    scale_fill_gradient(low= "#56B1F7", high = "#132B43")

enter image description here

  • how use my path for fie? – user1088259 May 26 at 11:00
  • nc = st_read(system.file("~/map/tyumen.shp", package="sf")) this code not working – user1088259 May 26 at 11:00
  • 1
    @user1088259 use st_read("~/map/tyumen.shp"), @Kazuhito used an example file to show you how to use this function – aldo_tapia May 27 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.