twitteR数据& ggplot2 - 如何绘制lat long of US only

时间:2017-03-23 23:09:56

标签: r ggplot2 twitter-streaming-api

我花了大约5-6个小时在网上搜索如何只在我的地图上显示美国拉丁美洲数据。目前,我有来自世界各地的拉丁点...我想做的只是美国的显示点。

另外,我是R的新手,并且一直在学习黑客代码。因此,如果有人也可以指出R的一些好的教程或课程,我会非常感激。

# Create happy Database
register_sqlite_backend("happy_tweets.db")
search_twitter_and_store('#happy', table_name = "tweets", lang = NULL,  locale = NULL, retryOnRateLimit = 120)
happy_tweets <- load_tweets_db(as.data.frame = TRUE, table_name = "tweets")

# Create sad Database
register_sqlite_backend("sad_tweets.db")
search_twitter_and_store('#sad', table_name = "tweets", lang = NULL,  locale = NULL, retryOnRateLimit = 120)
sad_tweets <- load_tweets_db(as.data.frame = TRUE, table_name = "tweets")

###########

library(ggplot2)
library(grid)
map.data <- map_data("world")

#extract lat and long only
happy_points <- data.frame(x = as.numeric(happy_tweets$lon), y = as.numeric(happy_tweets$lat), 
                           text = as.character(happy_tweets$text), screenName = as.character(happy_tweets$screenName))
sad_points <- data.frame(x = as.numeric(sad_tweets$lon), y = as.numeric(sad_tweets$lat), 
                         t = as.character(sad_tweets$text), screenName = as.character(sad_tweets$screenName))

# remove empty values
happy_points <- happy_points[rowSums(is.na(happy_points)) == 0,]
sad_points <- sad_points[rowSums(is.na(sad_points)) == 0,]

## plot on map
ggplot(map.data) + geom_map(aes(map_id = region), map = map.data, fill = "white", color = "grey70", size = 0.20) + 
        expand_limits(x = map.data$long, y = map.data$lat) + theme(axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), 
        axis.title = element_blank(), panel.background = element_blank(), panel.border = element_blank(), 
        panel.grid.major = element_blank(), plot.background = element_blank(),
        plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines")) + 
        geom_point(data = happy_points, aes(x = x, y = y), size = .3, alpha = 2/10, color = "blue") +
        geom_point(data = sad_points, aes(x = x, y = y), size = .3, alpha = 4/10, color = "red")

生成(图片): https://cloud.githubusercontent.com/assets/6144462/24273962/a34b188c-0fe2-11e7-8df6-972b3fcc84a2.png

如果我将我的map_data限制为states地图,只能在视觉上显示美国,但它很小,仍显示美国境外的所有点

Generages(图片): https://cloud.githubusercontent.com/assets/6144462/24273918/53ce2e52-0fe2-11e7-9763-52a363805966.png

提前致谢。

0 个答案:

没有答案