簡體   English   中英

從R中的頻率表制作折線圖

[英]Make line graph from frequency table in R

我制作了頻率表,以查看四年內種族類別的百分比。 使用以下代碼:

race <- table(crc$raceeth, crc$year)
perrace <- prop.table(race, 2)

我創建了一個如下表:

                      2014      2015      2016      2017      2018
  Other              0.1032609 0.1433232 0.1335762 0.1141788 0.1285297
  Latino             0.3913043 0.3339548 0.2173649 0.2321011 0.2434275
  non-hispanic black 0.3695652 0.3087858 0.3995143 0.4361254 0.4634859
  non-hispanic white 0.1358696 0.2139361 0.2495446 0.2175948 0.1645570

現在,我想創建一個折線圖,在x軸上有年份,在y上有每個種族/民族的線條,但是我不確定從這里開始

這是一個tidyverse方法:

library(tidyverse)
df %>%
    rownames_to_column("Group") %>%
    gather(x, y, -Group) %>%
    mutate(x = as.Date(gsub("X", "", x), format = "%Y")) %>%
    ggplot(aes(x, y, colour = Group)) +
    geom_line()

在此處輸入圖片說明


樣本數據

df <- read.table(text =
    "                    2014      2015      2016      2017      2018
  'Other'              0.1032609 0.1433232 0.1335762 0.1141788 0.1285297
  'Latino'             0.3913043 0.3339548 0.2173649 0.2321011 0.2434275
  'non-hispanic black' 0.3695652 0.3087858 0.3995143 0.4361254 0.4634859
  'non-hispanic white' 0.1358696 0.2139361 0.2495446 0.2175948 0.1645570", header = T, row.names = 1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM