2017-08-31 6 views
2

ベトナムの人口分布を示す棒グラフを作成しました。これは私のvietnam2015データです:ggplot2でたくさんの値を持つドットプロットを作成するには

Year Age.group Est.pop 
1 2015  0-4 7753 
2 2015  5-9 7233 
3 2015  10-14 6623 
4 2015  15-19 6982 
5 2015  20-24 8817 
6 2015  25-29 8674 
7 2015  30-34 7947 
8 2015  35-39 7166 
9 2015  40-44 6653 
10 2015  45-49 6011 
11 2015  50-54 5469 
12 2015  55-59 4623 
13 2015  60-64 3310 
14 2015  65-69 1896 
15 2015  70-74 1375 
16 2015  75-79 1162 
17 2015  80+ 1878 

これは私の棒グラフであると私はまた、代わりに棒グラフのドットプロットを作ることができれば、私は思っていました。

enter image description here

Library(tidyverse) 

vietnam2015 %>% 
    filter(Age.group != "5-9") %>% # Somehow this weird value creeped into the data frame, is therefor filtered out. 
    ggplot(aes(x = Age.group, y = Est.pop)) + 
    geom_col(colour = "black", 
      fill = "#FFEB3B") 

は今、私はドットプロットではなく、多くのデータポイントを持つデータのため通常である知っています。しかし、1つのドットが1,000人または100万を表すドットプロットを作成できますか?私はバーが人々で構成されていることをより良く伝えたい。 flowingdataの例と中央の画像のように:

Histogram explained

+1

あなたは() '' geom_dotplotで見たことがありますか? – aku

+0

はい、正しい数のbinwidthを見つけることができません。 'stat_bindot()bins = 30.を使用してbin_widthを使ってより良い値を選択する.'また、' geom_dotplot'ドキュメントには '...とドットが積み重なり、各ドットは一つの観測値を表します。 – Tdebeus

答えて

1

たぶん、あなたは、各Age.groupとプロットのためEst.pop件までゼロから値を生成することができます。しかし、私は他のより良い方法があると確信しています。

library(reshape2) 

df2 = dcast(data = df, Year~Age.group, value.var = "Est.pop") 

df3 = do.call(rbind, lapply(2:NCOL(df2), function(i) 
data.frame(Age.group = names(df2)[i], Est.pop = seq(0, df2[,i], 200)))) 

ggplot(data = df3[df3$Age.group != "5-9",], 
    aes(x = factor(Age.group), y = Est.pop)) + 
geom_point() 

enter image description here

DATA

df = structure(list(Year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
2015L, 2015L), Age.group = c("0-4", "5-9", "10-14", "15-19", 
"20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-54", 
"55-59", "60-64", "65-69", "70-74", "75-79", "80+"), Est.pop = c(7753L, 
7233L, 6623L, 6982L, 8817L, 8674L, 7947L, 7166L, 6653L, 6011L, 
5469L, 4623L, 3310L, 1896L, 1375L, 1162L, 1878L)), .Names = c("Year", 
"Age.group", "Est.pop"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17")) 
+0

あなたの答えをありがとうが、それは私が心に持っていたものではありません。多分、個々の「Age.group」のためにドットの列を2つ並べる方法がありますか?私は今、迂回路を考えています。おそらく、 'library(waffle)'パッケージが私たちを助けることができます。 – Tdebeus

1

我々はgeom_dotplotを使用することができます。あなたが言及したように、ドットプロットは通常小さなカウント数ですが、我々はデータを集約することができます。次のコードではmutate(Est.pop = round(Est.pop, digits = -3)/1000)を使用してEst.popを千に分割し、1000で除算した後、列で何回計算したかをそれぞれAge.groupと繰り返します。最後に、geom_dotplotを使用してデータをプロットしました。各ドットは1000人を表します。この視覚化は主にドット数に焦点を当てていると思うので、y軸は隠されています。

# Load package 
library(tidyverse) 

# Process the data 
dt2 <- dt %>% 
    mutate(Est.pop = round(Est.pop, digits = -3)/1000) %>% 
    split(f = .$Age.group) %>% 
    map_df(function(x) x[rep(row.names(x), x$Est.pop[1]), ]) 

# Plot the data 
ggplot(dt2, aes(x = Age.group)) + 
    geom_dotplot() + 
    scale_y_continuous(NULL, breaks = NULL) 

enter image description here

データ

dt <- read.table(text = " Year Age.group Est.pop 
1 2015  0-4 7753 
       2 2015  5-9 7233 
       3 2015  10-14 6623 
       4 2015  15-19 6982 
       5 2015  20-24 8817 
       6 2015  25-29 8674 
       7 2015  30-34 7947 
       8 2015  35-39 7166 
       9 2015  40-44 6653 
       10 2015  45-49 6011 
       11 2015  50-54 5469 
       12 2015  55-59 4623 
       13 2015  60-64 3310 
       14 2015  65-69 1896 
       15 2015  70-74 1375 
       16 2015  75-79 1162 
       17 2015  80+ 1878 ", 
       header = TRUE, stringsAsFactors = FALSE) 
関連する問題