【问题标题】:How to create a dot plot with a lot of values in ggplot2如何在 ggplot2 中创建具有大量值的点图
【发布时间】:2018-02-09 20:38:05
【问题描述】:

我创建了一个条形图来显示越南的人口分布。这是我的vietnam2015 数据:

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

现在我知道点图通常用于数据点不多的数据。但是我可以创建一个点图,其中一个点代表 1000 人或 100 万人吗?我喜欢更好地沟通酒吧由人组成。比如flowingdata的例子和中间图片:

【问题讨论】:

  • 你看过geom_dotplot()吗?
  • 是的,但我似乎找不到合适的 binwidth 数。我收到一个错误:stat_bindot() using bins = 30. Pick better value with binwidth. 还有geom_dotplot 文档说...and dots are stacked, with each dot representing one observation

标签: r plot ggplot2 histogram bar-chart


【解决方案1】:

我们可以使用geom_dotplot。正如您所提到的,点图通常用于小计数,但我们可以汇总数据。在下面的代码中,我使用mutate(Est.pop = round(Est.pop, digits = -3)/1000)Est.pop 舍入到千位,然后除以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)

数据

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)

【讨论】:

    【解决方案2】:

    也许您可以为每个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()
    

    数据

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

    【讨论】:

    • 感谢您的回答,但这并不是我真正想到的。也许有一种方法可以为每个人绘制几行粗点Age.group ....?我现在想绕路,也许library(waffle)包可以帮到我们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 2021-11-05
    • 2014-10-16
    • 1970-01-01
    相关资源
    最近更新 更多