R语言的数据集列的直方图或BarPlot

时间:2018-10-16 18:25:20

标签: r csv histogram

输入:我有一个用逗号分隔的CSV,其中各列的年龄介于0到100岁之间,第二行是按年龄划分的总人口。有更多的行,但是在这种情况下数据不相关。

enter image description here

输出:我想用R语言创建一个直方图,其中x轴显示人员的年龄,y轴显示该年龄的总人口。

我有一些代码,可以读取一个csv文件并显示我要代表的csv文件的一部分。

```{r}
v_file <- "../20181022_Ejercicio_1/DATASET.csv"
data <- read.csv(file=v_file, sep=',', header = TRUE)

edades <- data[1,2:101]
edades
```

enter image description here

该表显示了总人口与年龄的关系。

我的数据集的更多信息...

dput(edades[, 1:50])
structure(list(X0 = 13633L, X1 = 13918L, X2 = 13712L, X3 = 13533L, 
    X4 = 14018L, X5 = 13968L, X6 = 13801L, X7 = 13747L, X8 = 14288L, 
    X9 = 13588L, X10 = 13849L, X11 = 13612L, X12 = 13630L, X13 = 13456L, 
    X14 = 13118L, X15 = 12776L, X16 = 13284L, X17 = 13213L, X18 = 13051L, 
    X19 = 13771L, X20 = 13903L, X21 = 14450L, X22 = 15543L, X23 = 16714L, 
    X24 = 18644L, X25 = 19330L, X26 = 20726L, X27 = 21812L, X28 = 22323L, 
    X29 = 22801L, X30 = 23531L, X31 = 23952L, X32 = 24602L, X33 = 25041L, 
    X34 = 25887L, X35 = 26276L, X36 = 26918L, X37 = 26872L, X38 = 27608L, 
    X39 = 27503L, X40 = 27756L, X41 = 27653L, X42 = 26994L, X43 = 26239L, 
    X44 = 25364L, X45 = 24818L, X46 = 24194L, X47 = 23432L, X48 = 23152L, 
    X49 = 23074L), .Names = c("X0", "X1", "X2", "X3", "X4", "X5", 
"X6", "X7", "X8", "X9", "X10", "X11", "X12", "X13", "X14", "X15", 
"X16", "X17", "X18", "X19", "X20", "X21", "X22", "X23", "X24", 
"X25", "X26", "X27", "X28", "X29", "X30", "X31", "X32", "X33", 
"X34", "X35", "X36", "X37", "X38", "X39", "X40", "X41", "X42", 
"X43", "X44", "X45", "X46", "X47", "X48", "X49"), class = "data.frame", row.names = 1L)

1 个答案:

答案 0 :(得分:1)

将数据转换为常用数据(在列而不是行中):

age_df = data.frame(t(edades), as.numeric(sub("X", "", names(edades))))
names(age_df) = c("n", "age")

以基为单位的图

barplot(height = age_df$n, names.arg = age_df$age)

enter image description here

或使用ggplot进行绘图:

library(ggplot2)
ggplot(age_df, aes(x = age, y = n)) +
  geom_col()

enter image description here