2017-08-31 25 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 

這是我的條形圖,我在想,如果我也可以做點圖而不是條形圖。

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") 

enter image description here

現在我知道散點圖通常是與沒有那麼多的數據點的數據。但是,我可以創建一個點表示一個點代表1000人或一百萬個點的點圖嗎?我喜歡更好地溝通,酒吧由人組成。像flowingdata的例子,中間的圖像:

Histogram explained

+1

你有看'geom_dotplot()'? – aku

+0

是的,但我似乎無法找到正確數量的binwidth。我得到一個錯誤:'stat_bindot()使用bin = 30。用binwidth選擇更好的值.'還有'geom_dotplot'文檔說'...和點堆疊,每個點表示一個觀察值。 – Tdebeus

回答

1

也許你可以生成從零高達的Est.pop每個Age.group和情節值。但我相信還有其他更好的方法。

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'繪製幾行粗點行....?我現在想着繞道而行,也許'圖書館(華夫餅)'包可以幫助我們。 – Tdebeus

1

我們可以使用geom_dotplot。正如你所提到的那樣,點圖通常用於小計數,但我們可以彙總數據。在下面的代碼中,我使用mutate(Est.pop = round(Est.pop, digits = -3)/1000)Est.pop四捨五入爲1000,然後除以1000.之後,我重複每個Age.group多少次我在Est.pop列中計算。最後,我用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) 
相關問題