简体   繁体   English

使用ggplot(区域图)在R中重现图

[英]Reproducing plot in R using ggplot (Area plot)

I'm trying to reproduce a plot in R where I have the data but not the code. 我正在尝试在R中重现一个情节,我有数据而不是代码。 I think using ggplot will be the best solution. 我认为使用ggplot将是最好的解决方案。

The plot should look like this: 情节应如下所示:

点击

My data: 我的数据:

A dataframe (d) where column 1 ("Year") gives the year (1960 - 2000) and 15 other columns ("Group 1", "Group 2"...) giving the value of the group. 数据框(d),其中第1列(“年”)给出年份(1960 - 2000),其他15列(“第1组”,“第2组”......)给出该组的值。 Thus, each row contains the year and the values for the certain group. 因此,每行包含特定组的年份和值。 (eg (1) 1960; 455; 367; 477; 788; 456; 334; 456;...) (例如(1)1960; 455; 367; 477; 788; 456; 334; 456; ......)

I already tried some experiments with geom_area, but this does not result in the intendet graph. 我已经尝试过使用geom_area进行一些实验,但这并不会产生用户图。

ggplot(d, aes(x = Year, y = d[,2]))+
  geom_area(fill = "#00AFBB", color = "blue")

Here I get n (n = # of Years) very thin bars representing the value of group 1 against a certain year. 在这里,我得到n(n =#Years)非常细的条形,代表第1组对某一年的价值。

Hope you can help me out maybe with some ideas or a tutorial. 希望你能帮我解决一些想法或教程。 Is area plot the right choice for what I want to do? 区域图是我想要做的正确选择吗?

Best! 最好!

You need to convert the data from wide to long format and then it should work: 您需要将数据wide格式转换为long格式然后它应该工作:

library(dplyr)
library(tidyr)
library(ggplot2)
d <- gather(d, Group, value, Group1:Group15)
ggplot(d, aes(x = Year, y = value, fill = Group))+
  geom_area()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM