Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

ggplot2 - NA issues with R. How can I fixed this error in R?

library(tidyverse) 
library(sp)
setwd("C:/Users/sy/Desktop/Sy_project")
TUR <- readRDS("TUR_adm1.rds")

plot(TUR)

TUR@data %>% as_tibble() %>% head(10)
ggplot(TUR, aes(x = long, y = lat)) +
  geom_polygon()

ggplot(TUR, aes(x = long, y = lat)) +
  geom_polygon(aes(group = group)) +
  coord_fixed()
TUR_for <- fortify(TUR)
head(TUR_for)
ggplot(TUR_for) + 
  geom_polygon(aes(x = long, y = lat,
                   group = group),
               color = "white",
               fill = "#97ECEA") +
  theme_void() + 
  coord_fixed()

data  =  read.table('DSE501_fall2020_HW3_.csv', header =T, sep=",")
data
options(max.print = 999999)


data_map = data[16, 2:76]
data_map

data_map2<-as.data.frame(t(data_map))
data_map2

library(tibble)
library(dplyr)
data_map2<-data_map2 %>%
  rownames_to_column("cities")
data_map2

names(data_map2)[2] <- "potato_pro"
data_map2

id_and_cities <- data_frame(id = rownames(TUR@data),
                                cities = TUR@data$NAME_1) %>% 
  left_join(data_map2, by = "cities")

TUR_mat <- fortify(TUR)

id_and_cities %>% head()

final_map <- left_join(TUR_for, id_and_cities, by = "id")

final_map %>% head()

ggplot(final_map) +
  geom_polygon( aes(x = long, y = lat, group = group, fill = potato_pro),
                color = "grey") +
  coord_map() +
  theme_void() + 
  labs(title = "Türkiye'deki patates üretimi",
       subtitle = paste0("Toplam Say?: ", sum(data_map2$potato_pro, na.rm = TRUE)),
       caption = "Kaynak: Türkiye ?statistik Kurumu") +
  scale_fill_distiller(name = "Patates üretimi",
                       palette = "Spectral", limits = c(0,720000), na.value = "black") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))

I use TUIK potato production data set. I want to visualize the annual potato production in year 2019 on Turkey mapI have a question about the this code. When I checked the data I have 6 NA values. However, when I runned the code, I have almost 30 NA. How can I solve this error in R?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...