r - Neat way to plot this correlation using ggplot2?
问题描述
I have this dataset
airline avail_seat_km_per_week Number Year
1: Aer Lingus 320906734 2 1985-99
2: Aeroflot* 1197672318 76 1985-99
3: Aerolineas Argentinas 385803648 6 1985-99
4: Aeromexico* 596871813 3 1985-99
5: Air Canada 1865253802 2 1985-99
---
108: United / Continental* 7139291291 14 2000-14
109: US Airways / America West* 2455687887 11 2000-14
110: Vietnam Airlines 625084918 1 2000-14
111: Virgin Atlantic 1005248585 0 2000-14
112: Xiamen Airlines 430462962 2 2000-14
These are some instances of the dataset:
data.frame(airline=c("Aer Lingus", "Aeroflot*", "Aerolineas Argentinas", "Aeromexico*", "Air Canada", "Aer Lingus", "Aeroflot*", "Aerolineas Argentinas", "Aeromexico*", "Air Canada"), Number=c(2, 76, 6, 3, 2,0 ,6,1,5,2), Year=c("1985-99", "1985-99", "1985-99", "1985-99", "1985-99", "2000-14", "2000-14", "2000-14", "2000-14", "2000-14"))
which includes the number of crashes of airlines around the world in 2 different periods, 85-99 and 00-14, I want to plot a scatterplot that displays the number of crashes in period 85-99 against period 00-14, what is a neat way to do it using dplyr and ggplot2 packages, preferably using pipes?.
Please let me know if there are something I could do to further specify the problem. Appreciate your help!
解决方案
在寻求有关绘图ggplot
的帮助时,如果您非常清楚每个维度(x、y、颜色等)的数据是什么,这将很有帮助。
library(tidyr)
library(ggplot2)
# (calling your data d)
d %>%
# widen the data so each plot dimension gets a column
pivot_wider(names_from = Year, values_from = Number) %>%
# use backticks for non-standard column names (because of the dash in this case)
ggplot(aes(x = `1985-99`, y = `2000-14`, color = airline)) +
geom_point()
推荐阅读
- java - DirectoryStream 比 Files.listFiles() 快吗?
- java - 使用多个线程搜索列表并查找元素(不使用并行流)
- java - 为不同的消费者提供不同的 bean
- kubernetes - Kubernetes - calico IP_AUTODETECTION_METHOD 无法设置
- python - 从国际象棋导入国际象棋引擎:国际象棋引擎错误
- python - 计算每个组内的元素数
- go - 为什么我不能使用 DefaultClient 访问日历 API?
- python-3.x - Python如何检查字符串是UTC时间戳?
- c# - Razor Pages PageModel 返回 Null 属性
- c# - 系统找不到使用 NUnit3 控制台和 testparam 指定的文件