【问题标题】:R: Guessing the "Format" of a Dataset?R:猜测数据集的“格式”?
【发布时间】:2022-12-20 04:13:19
【问题描述】:

我正在使用 R 编程语言。

我正在尝试在此处按照本教程进行操作https://rpubs.com/chidungkt/505486,但本教程所需的数据集似乎不再可用。因此,我试图“猜测”数据集的格式并尝试以类似的格式模拟假数据集 - 从而让我继续教程。

我花了一些时间尝试分析代码的结构并尝试推断数据集的格式——这就是我想出的:

Age = c("0-10", "0-10", "11-20", "11-20", "21-30", "21-30", "31-40", "31-40", "41-50", "41-50", "51-60", "51-60")

Gender = c("M", "F", "M", "F", "M", "F", "M", "F", "M", "F", "M", "F")

Value = as.integer(rnorm(12, 100,10))

vn_2018_pop = data.frame(Age, Gender, Value)

     Age Gender Value
1   0-10      M   125
2   0-10      F   103
3  11-20      M    84
4  11-20      F   105
5  21-30      M    96
6  21-30      F    88
7  31-40      M    88
8  31-40      F   120
9  41-50      M   106
10 41-50      F   118
11 51-60      M   105
12 51-60      F   112

基于这个数据集,我尝试运行教程中的 R 代码:

# Load some packages for scrapping data and data manipulation: 
library(rvest)
library(magrittr)
library(tidyverse)
library(extrafont)

my_colors <- c("#2E74C0", "#CB454A")
my_font <- "Roboto Condensed"

vn_2018_pop %>% ggplot(aes(Age, Value, fill = Gender)) + 
  geom_col(position = "stack") + 
  coord_flip() + 
  scale_y_continuous(breaks = seq(-5000000, 5000000, 1000000), 
                     limits = c(-5000000, 5000000), 
                     labels = c(paste0(seq(5, 0, -1), "M"), paste0(1:5, "M"))) + 
  theme_minimal() + 
  scale_fill_manual(values = my_colors, name = "", labels = c("Female", "Male")) + 
  guides(fill = guide_legend(reverse = TRUE)) + 
  theme(panel.grid.major.x = element_line(linetype = "dotted", size = 0.2, color = "grey40")) + 
  theme(panel.grid.major.y = element_blank()) + 
  theme(panel.grid.minor.y = element_blank()) + 
  theme(panel.grid.minor.x = element_blank()) + 
  theme(legend.position = "top") + 
  theme(plot.title = element_text(family = my_font, size = 28)) + 
  theme(plot.subtitle = element_text(family = my_font, size = 13, color = "gray40")) + 
  theme(plot.caption = element_text(family = my_font, size = 12, colour = "grey40", face = "italic")) + 
  theme(plot.margin = unit(c(1.2, 1.2, 1.2, 1.2), "cm")) + 
  theme(axis.text = element_text(size = 13, family = my_font)) + 
  theme(legend.text = element_text(size = 12, face = "bold", color = "grey30", family = my_font)) + 
  labs(x = NULL, y = NULL, 
       title = "Population Pyramids of Vietnam in 2018",
       subtitle = "A population pyramid illustrates the age-sex structure of a country's population and may provide insights about\npolitical and social stability, as well as economic development. Countries with young populations need to\ninvest more in schools, while countries with older populations need to invest more in the health sector.",
       caption = "Data Source: https://www.census.gov")

代码似乎运行了——但产生了一个空图:

有人可以告诉我我做错了什么以及我可以做些什么来解决这个问题吗?

谢谢!

【问题讨论】:

    标签: r visualization data-manipulation


    【解决方案1】:

    通过将值乘以例如4e4 并使男性的价值为负:

    library(tidyverse)
    library(extrafont)
    
    set.seed(123)
    
    vn_2018_pop$Value <- 4e4 * vn_2018_pop$Value
    vn_2018_pop$Value[vn_2018_pop$Gender == "M"] <- -vn_2018_pop$Value[vn_2018_pop$Gender == "M"]
    
    my_colors <- c("#2E74C0", "#CB454A")
    my_font <- "Roboto Condensed"
    
    vn_2018_pop %>% ggplot(aes(Age, Value, fill = Gender)) +
      geom_col(position = "stack") +
      coord_flip() +
      scale_y_continuous(
        breaks = seq(-5000000, 5000000, 1000000),
        limits = c(-5000000, 5000000),
        labels = c(paste0(seq(5, 0, -1), "M"), paste0(1:5, "M"))
      ) +
      theme_minimal() +
      scale_fill_manual(values = my_colors, name = "", labels = c("Female", "Male")) +
      guides(fill = guide_legend(reverse = TRUE)) +
      theme(panel.grid.major.x = element_line(linetype = "dotted", size = 0.2, color = "grey40")) +
      theme(panel.grid.major.y = element_blank()) +
      theme(panel.grid.minor.y = element_blank()) +
      theme(panel.grid.minor.x = element_blank()) +
      theme(legend.position = "top") +
      theme(plot.title = element_text(family = my_font, size = 28)) +
      theme(plot.subtitle = element_text(family = my_font, size = 13, color = "gray40")) +
      theme(plot.caption = element_text(family = my_font, size = 12, colour = "grey40", face = "italic")) +
      theme(plot.margin = unit(c(1.2, 1.2, 1.2, 1.2), "cm")) +
      theme(axis.text = element_text(size = 13, family = my_font)) +
      theme(legend.text = element_text(size = 12, face = "bold", color = "grey30", family = my_font)) +
      labs(
        x = NULL, y = NULL,
        title = "Population Pyramids of Vietnam in 2018",
        subtitle = "A population pyramid illustrates the age-sex structure of a country's population and may provide insights about
    political and social stability, as well as economic development. Countries with young populations need to
    invest more in schools, while countries with older populations need to invest more in the health sector.",
        caption = "Data Source: https://www.census.gov"
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      相关资源
      最近更新 更多