首页 > 解决方案 > 更好地组织图表上的数据

问题描述

我的数据组织为

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 个国家,并且使图表看起来非常小。我想知道是否有一种方法可以使图表看起来更大或显示数据的更好方法?

标签: rggplot2country-codes

解决方案


Jared 的出色回答中,一些标签确实重叠,例如巴巴多斯和阿根廷。这可以使用包解决(请参阅详细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"))

在此处输入图像描述


推荐阅读