[英]Using ggplot geom_line to visualize a difference between two years
I am trying to visualize a data set with number of countries, each having a variable for two years and a value for each of the year (world press freedom index). 我正在尝试可视化包含多个国家/地区的数据集,每个国家/地区都有两年的变量,并且每年都有一个值(世界新闻自由指数)。 I already search through answers at stackoverflow and other sites but I a could not find anything that would help me with this. 我已经在stackoverflow和其他站点上搜索了答案,但是我找不到任何可以帮助我解决这个问题的方法。 This is the data set after using ddplyr melt on it: 这是在其上使用ddplyr熔解后的数据集:
pfindex2narrow = reshape2::melt(pfindex2, id.vars = 'Origin')
pfindex2narrow
Origin variable value
1 Eritrea 2014 84.86
2 NorthKorea 2014 83.25
3 Turkmenistan 2014 80.83
4 Syria 2014 77.29
5 China 2014 73.55
6 Vietnam 2014 72.63
7 Sudan 2014 72.34
8 Iran 2014 72.32
9 Somalia 2014 72.31
10 Laos 2014 71.25
11 Djibouti 2014 71.04
12 Cuba 2014 70.21
13 Yemen 2014 66.36
14 EquatorialGuinea 2014 66.23
15 Uzbekistan 2014 61.14
16 SaudiArabia 2014 59.41
17 Bahrain 2014 58.69
18 Azerbaijan 2014 58.41
19 Rwanda 2014 56.57
20 Libya 2014 45.99
21 Eritrea 2013 84.83
22 NorthKorea 2013 81.96
23 Turkmenistan 2013 80.81
24 Syria 2013 77.04
25 China 2013 72.91
26 Vietnam 2013 72.36
27 Sudan 2013 71.88
28 Iran 2013 72.29
29 Somalia 2013 73.19
30 Laos 2013 71.22
31 Djibouti 2013 70.34
32 Cuba 2013 70.92
33 Yemen 2013 67.26
34 EquatorialGuinea 2013 67.95
35 Uzbekistan 2013 61.01
36 SaudiArabia 2013 58.30
37 Bahrain 2013 58.26
38 Azerbaijan 2013 52.87
39 Rwanda 2013 56.57
40 Libya 2013 39.84
The goal is to visualize the difference between each year's index and show whether it is following a decreasing or increasing trend. 目的是可视化每年指数之间的差异,并显示其是遵循下降趋势还是上升趋势。 My own attempt is below. 我自己的尝试如下。 I am trying to use ggplot2 to visualize this, however, as you can see there are few issues (ie lines seem to be arbitrary and are not relating to real index values). 我正在尝试使用ggplot2对此进行可视化,但是,正如您所看到的,几乎没有问题(即,行似乎是任意的,并且与实际索引值无关)。
b = ggplot(pfindex2narrow, aes(x = variable, y = value, group = Origin)) +
geom_line() +
geom_text(aes(label=value, hjust = 0.5), size = 4)
b + facet_wrap(~ Origin, ncol = 2) +
theme(
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust = 0.5),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
And this is the output: 这是输出:
Sadly, I ran out of ideas how to fix this and I am kinda stuck. 可悲的是,我没有解决该问题的想法,而且我有些固执。 Maybe you have any ideas how to approach this. 也许您对如何解决这个问题有任何想法。
Thank you! 谢谢!
Here is a few options: 这里有一些选择:
First, let's shorten the data name a bit for less typing and easier reading. 首先,让我们略微缩短数据名称,以减少键入次数并更易于阅读。 I also reordered the data, so it's easier to get a quick grasp of what's going on. 我还对数据进行了重新排序,因此更容易快速了解发生的情况。
d <- pfindex2narrow
d$variable <- factor(d$variable)
d$Origin <- factor(
d$Origin,
levels = (d$Origin)[rev(order(d$value[d$variable == '2014']))]
)
We can make a line plot, but it is quite confusing to look at: 我们可以画线图,但是看起来很混乱:
ggplot(d, aes(x = variable, y = value, col = Origin, group = Origin)) +
geom_line(size = 1) +
scale_x_discrete(expand = c(0.1, 0), limits = c('2013', '2014', 'country')) +
theme_bw()
I'm not really a fan. 我不是粉丝。 (It could look much better, if we would plot the ranks of countries instead.) (如果我们改为绘制国家/地区的排名,它看起来可能会好得多。)
Perhaps we can use bars instead: 也许我们可以改用酒吧:
ggplot(d, aes(x = Origin, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'dodge') +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
Already better! 已经更好了! But bars to 0 are perhaps not utilizing our space very well. 但是0的小节可能无法很好地利用我们的空间。 Another option would be to use points. 另一种选择是使用积分。
d2 <- d
d2$x <- as.numeric(d2$Origin) + ifelse(d2$variable == '2013', -0.25, 0.25)
ggplot(d2, aes(x = Origin, y = value, col = variable)) +
geom_point(position = position_dodge(w = 1)) +
geom_line(aes(x = x, group = Origin), col = 1) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
And finally, if only the change is important, and not so much the absolute value, we can express that instead: 最后,如果只有更改很重要,而不是绝对值,那么我们可以这样表达:
library(dplyr)
d3 <- d %>%
group_by(Origin) %>%
arrange(variable) %>%
summarize(dif = diff(value)) %>%
arrange(dif)
d3$Origin <- factor(d3$Origin, levels = unique(d3$Origin))
ggplot(d3, aes(Origin, dif, fill = Origin)) +
geom_bar(stat = 'identity', position = 'identity') +
coord_flip() +
theme_minimal() +
guides(fill = 'none') +
xlab('') + ylab('change from 2013 to 2014')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.