r - 更好地组织图表上的数据
问题描述
我的数据组织为
Country Year Region1 Region2...
Mexico 2007 10000 20000
Mexico 2008 1000. 1000
Guatemala 2007 500 2000
Guatemala 2008 1000 200
x <- read_csv("Downloads/BorderYearData.csv", skip = 1)
x[is.na(x)] = 0
x$CITIZENSHIP <- str_to_title(x$CITIZENSHIP) #Fixes Country Name from CAPS to Title Format
x$Region <- countrycode(sourcevar = x$CITIZENSHIP, origin = "country.name", destination = "region") #Applies a region to each country
Latin_America <- x %>% filter(Region == "Latin America & Caribbean")
South_West <- Latin_America #Only includes Latin American countries from 2007-2019
ggplot(South_West, aes(x = Year, y = YUM, col = CITIZENSHIP)) + geom_line() + geom_point() + scale_y_log10() + ggtitle("Yuma Apprehensions By Country") + xlab("Year") + ylab("Apprehensions") #Plots the Years vs. Yuma Region filtered by Latin American countries
年份对比 Yuma Region 按拉丁美洲国家过滤 我面临的主要问题是我创建的图表有太多标签,因为它需要显示大约 42 个国家,并且使图表看起来非常小。我想知道是否有一种方法可以使图表看起来更大或显示数据的更好方法?
解决方案
在Jared 的出色回答中,一些标签确实重叠,例如巴巴多斯和阿根廷。这可以使用ggrepel包解决(请参阅详细ggrepel
示例页面)。
基于Jared 的回答和数据,
library(ggplot2)
library(ggrepel)
library(dplyr)
ggplot(South_West, aes(x = Year, y = YUM, colour = CITIZENSHIP)) +
geom_line() +
geom_point() +
geom_label_repel(data = . %>% filter(Year == max(Year)),
aes(x = Year, y = YUM, label = CITIZENSHIP),
xlim = c(-Inf, Inf),
segment.curvature = -0.1,
nudge_x = 0.3, direction = "y", hjust = "left") +
coord_cartesian(expand = TRUE, clip = "off") +
scale_y_log10() +
scale_x_continuous(breaks = scales::pretty_breaks(10)) +
scale_colour_discrete(guide = "none") +
ggtitle("Yuma Apprehensions By Country") +
xlab("Year") +
ylab("Apprehensions") +
theme(plot.margin = unit(c(1,6,1,1), "lines"))
推荐阅读
- python - Thread won't stop until window is destroyed, window won't destroy unless thread is stopped
- docker - Fluentbit kubernetes - 如何在 /var/log/ 中存在的应用程序日志中添加 kubernetes 元数据
/ 小路 - python - python tkinter如何使用画布找到特定列表中最近的项目
- java - 未找到 Maven 模块:javafx.fxml
- laravel - 如何使用查询字符串重定向命名路由?
- php - 使用 php 将商品添加到购物车
- docker-registry - 来自守护进程的错误响应:Get https://192.168.1.5/v2/: x509: certificate signed by unknown authority
- python - 为什么表格在邮寄时无效
- php - SendGrid marketingdb php 出现“禁止访问”错误
- python - Pycharm 不解析部分包