无法在R中正确绘制data.frame

时间:2020-04-24 13:20:02

标签: r data-science

我无法自己完成,至少尝试了2个小时...

我从CSV文件导入了以下data.frame:

> asyl_continents
          2010 2011 2012 2013 2014  2015  2016
Europe    1411 1352 2047 3277 5105  7647  5296
Asia      2832 3229 3903 4418 7123 32152 18002
America     20   17   20   27   35    42    97
Africa     436  757  897 1497 3846  5412  4592
Australia    0    0    0    0    0     0     1
stateless   34   34   35   28   80    88   179
unknown    159  160  140  207  360   408   505

这是它的输出:

structure(list(`2010` = c(1411L, 2832L, 20L, 436L, 0L, 34L, 159L
), `2011` = c(1352L, 3229L, 17L, 757L, 0L, 34L, 160L), `2012` = c(2047L, 
3903L, 20L, 897L, 0L, 35L, 140L), `2013` = c(3277L, 4418L, 27L, 
1497L, 0L, 28L, 207L), `2014` = c(5105L, 7123L, 35L, 3846L, 0L, 
80L, 360L), `2015` = c(7647L, 32152L, 42L, 5412L, 0L, 88L, 408L
), `2016` = c(5296L, 18002L, 97L, 4592L, 1L, 179L, 505L)), class = "data.frame", row.names = c("Europe", 
"Asia", "America", "Africa", "Australia", "stateless", "unknown"
))

我想绘制以下内容:https://i.stack.imgur.com/l7s6l.png 在x轴上应该有年份,并且对于每个类别(欧洲,亚洲,...,未知),都应该有单独的行。

想知道这是什么吗?对于一个学生项目,我想表明,随着更多难民的到来,犯罪率仅以绝对数字上升,而相对数字没有上升。希望以后再将犯罪率与这个数据集联系起来。

3 个答案:

答案 0 :(得分:1)

以下代码不依赖于非base-R软件包。

A)graphics::matplot绘制数据帧自动强制到的矩阵的列:

matplot(x = as.numeric(colnames(asyl_continents)), # x values: the years
    y    = t(asyl_continents),                 # y values: one column per plotted line each, therefore transpose years' asylum values to columns 
    # col= c(...)                              # could specify your own colors here, as well as line types, thickness etc.
    type = "l",                                # specify type: draw lines, see ? matplot for details
    xlab = "years", ylab = "asylum")

B)legend()添加了图例

legend(x="topleft", legend = row.names(asyl_continents), 
       col = seq_len(ncol(asyl_continents)),      # use matplot's default color sequencs, unless spefified otherwise in matplot() (see https://stackoverflow.com/questions/27796583/how-to-add-colour-matched-legend-to-a-r-matplot)
       lwd = 1, cex=0.85)

您可以根据需要更改颜色,线型等,请参见? matplot了解详情。

enter image description here

答案 1 :(得分:0)

欢迎来到stackoverflow。请再说明一下您想要作为一个小工具吗?另外,请提供dput(asyl_continents)的输出,以便我们可以直接使用您的数据框。以下代码是“按内存”,我没有对其进行测试,但我认为我会工作。

类似boxplot(asyl_continents)的东西吗?

按相对数,您的意思是按行递增?确实,这是有意义的:

df <- asyl_continents
df <- apply(df,1,function(x){1+ (x-x[1])/x[1]})
matplot(t(df))

答案 2 :(得分:0)

这应该使您朝着整齐的方向出发,以实现您渴望获得的折线图。


library(tibble)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)


# Provide a sample of data in a proper dataframe or equivalent object

df <- tibble(cont = c("Europe", "Asia", "America", "Africa", "Australia", "stateless", "unknown"), 
             `2010` = c(1411L, 2832L, 20L, 436L, 0L, 34L, 159L), 
             `2011` = c(1352L, 3229L, 17L, 757L, 0L, 34L, 160L),
             `2012` = c(2047L, 3903L, 20L, 897L, 0L, 35L, 140L), 
             `2013` = c(3277L, 4418L, 27L, 1497L, 0L, 28L, 207L),
             `2014` = c(5105L,7123L, 35L, 3846L, 0L, 80L, 360L),
             `2015` = c(7647L, 32152L, 42L, 5412L, 0L, 88L, 408L), 
             `2016` = c(5296L, 18002L, 97L, 4592L,1L, 179L, 505L))

# in this case it is helpful to put the data in to longer format 
df1 <- 
  df %>% 
  pivot_longer(cols = matches("\\d{4}$"), names_to = "year", values_to = "asyl_nr")

# plot the data
ggplot(df1, aes(year, asyl_nr, colour = cont, group = cont))+
  geom_line()+
  theme_classic()

这就是你最终得到的:

enter image description here