2016-05-14 3 views
1

私はこのようなデータフレームがあります。プロット値

  • は、X軸上YEAR列の値
  • のがあります:私はことをグラフに取得したい

    > dput(df) 
    structure(list(OBBLIGATORIO = structure(c(2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("no", 
    "yes"), class = "factor"), COUNTRY = structure(c(16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
    16L, 16L, 16L, 16L), .Label = c("Austria", "Belgium", "Bulgaria", 
    "Croatia", "Cyprus", "Czech Republic", "Denmark", "Estonia", 
    "Finland", "France", "Germany", "Greece", "Hungary", "Iceland", 
    "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", 
    "Norway", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", 
    "Spain", "Sweden", "United Kingdom of Great Britain and Northern Ireland" 
    ), class = "factor"), YEAR = c(2003L, 2006L, 2007L, 2008L, 2009L, 
    2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 
    2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 
    1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 
    2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 
    2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 
    1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 2007L, 
    2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 
    2001L, 2002L, 2003L, 2006L, 2007L, 2008L, 2009L, 2010L, 1995L, 
    1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2006L, 
    2007L, 2008L, 2009L, 2010L, 1995L, 1996L, 1997L, 1998L, 1999L, 
    2000L, 2001L, 2002L), AGE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Total", class = "factor"), 
        `CAUSE OF DEATH` = c("Acute poliomyelitis", "Acute poliomyelitis", 
        "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", 
        "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", 
        "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", 
        "Acute poliomyelitis", "Acute poliomyelitis", "Acute poliomyelitis", 
        "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", 
        "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", 
        "Diphtheria", "Diphtheria", "Diphtheria", "Diphtheria", "Measles", 
        "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", 
        "Measles", "Measles", "Measles", "Measles", "Measles", "Measles", 
        "Measles", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", 
        "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", "Tetanus", 
        "Tetanus", "Tetanus", "Tetanus", "Tuberculosis", "Tuberculosis", 
        "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", 
        "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", 
        "Tuberculosis", "Tuberculosis", "Tuberculosis", "Tuberculosis", 
        "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", 
        "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", 
        "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", 
        "Viral hepatitis", "Viral hepatitis", "Viral hepatitis", 
        "Viral hepatitis", "Viral hepatitis", "Whooping cough", "Whooping cough", 
        "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", 
        "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough", 
        "Whooping cough", "Whooping cough", "Whooping cough", "Whooping cough" 
        ), VALUE = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 1L, 1L, 6L, 7L, 7L, 1L, 2L, 
        3L, 2L, 5L, 12L, 9L, 13L, 9L, 13L, 8L, 17L, 14L, 16L, 18L, 
        15L, 19L, 11L, 10L, 25L, 24L, 21L, 22L, 23L, 20L, 34L, 32L, 
        31L, 30L, 29L, 28L, 27L, 26L, 41L, 42L, 43L, 45L, 46L, 47L, 
        33L, 35L, 36L, 37L, 38L, 39L, 40L, 44L, 1L, 2L, 1L, 1L, 1L, 
        2L, 2L, 2L, 1L, 3L, 1L, 1L, 1L, 1L), .Label = c("0", "1", 
        "2", "3", "6", "7", "9", "17", "18", "19", "21", "22", "27", 
        "28", "30", "31", "37", "41", "42", "301", "329", "333", 
        "344", "350", "396", "413", "415", "460", "517", "558", "597", 
        "609", "622", "647", "681", "1087", "1349", "1413", "1448", 
        "1499", "1576", "1654", "1725", "1948", "2531", "2665", "2757" 
        ), class = "factor"), ID = 1:98), .Names = c("OBBLIGATORIO", 
    "COUNTRY", "YEAR", "AGE", "CAUSE OF DEATH", "VALUE", "ID"), row.names = c(NA, 
    -98L), class = "data.frame") 
    

    をy軸があります

  • VALUE列のデータからの値を死亡原因列で除算します
だから、

ような何か:

x11() 
ggplot(df, aes(x = df$`YEAR`, y = df$`VALUE`, fill = df$`CAUSE OF DEATH`, colour = df$`CAUSE OF DEATH`)) + 
    geom_density(alpha = 0.1) + 
    xlim(1995, 2010) 

しかし、結果は私が欲しいものとは完全に異なっている:私がしようと enter image description here

おかげ

+1

あなたは 'ggplot(DFを実行する必要があり、AESは、(X = DFます$ YEAR、yは$'原因DFのDF $値、グループを= = DEATH'、color = df $ '死の原因))+ geom_line()' – Divi

+0

あなたの質問は不明です。死因はカテゴリに分類されます。どのように値を分類値で分けたいのですか?データの例を調整してください。それには、使用するすべての変数が含まれていることを確認してください。 – Thierry

答えて

1

は、私はあなたの実際の問題が何であるかわからないんだけど、あなたのデータフレームの一つの問題は、VALUE列が現在ない数値などとして、要因として定義されていることです。私はこれを是正することが、あなたの問題を解決するためには長い道のりになると思います。私はこれを事後的に(データフレームがすでに作成された後に)行いますが、read.table()または同様のコマンドでRにデータを取得する場合は、おそらくデータフレームの作成時にカラムのclassを指定することができますより良いアプローチ。

以下のコードでは、データフレームの操作にdplyrパッケージを使用しています。これはかなり強力ですが、この特定の例では、基底Rができなかったことは何もしていません。

require(ggplot2) 
require(dplyr) 
require(magrittr) 

df <- ### YOUR dput output goes here ### 

# fix the problem with the `VALUE` column 
df %<>% mutate(VALUE = VALUE %>% as.character %>% as.numeric) 

# equivalent in base R: 
# df$VALUE <- as.numeric(as.character(df$VALUE)) 

# make a graph (is it the one you want?) 
df %>% group_by(YEAR, `CAUSE OF DEATH`) %>% 
     summarize(value = sum(VALUE)) %>% 
     ggplot(aes(x = YEAR, y = value, color = `CAUSE OF DEATH`)) + 
      geom_line() + 
      theme_bw() + 
      geom_point() 

# save graph for uploading to SO 
ggsave('SO37230266.png') 

結果がこのグラフです:

enter image description here