首页 > 解决方案 > 如何根据提供的数据在 R 中重新创建下面的图表?

问题描述

我应该从下面提供的表格中创建一个数据框,然后我还想重新创建下面的图表。我已经创建了数据框,但我不知道如何重新创建图表。

表格和图表

这是我到目前为止的代码。

Householders <- c("Householders under 25","Householders 25 to 44",
              "Householders 45 to 64","Householders 65+")

MedianHouseholdIncome<-c(21835,38317,40424,23763)

MarginOfError <- c(3695,1609,2327,1592)

df<- data.frame(Householders,MedianHouseholdIncome,MarginOfError)

names(df)[2] <- "Median Household Income($)"
names(df)[3] <- "Margin of Error($)"

df$Householders <- as.factor(df$Householders)

View(df)

标签: r

解决方案


这是一个面向专业程序员的网站,因此您不太可能在这里获得所需的帮助。如果您在作业问题上遇到问题,最好的办法是询问您的课程主管/讲师/导师/等,他们可以帮助您解决问题。

因为您已经发布了自己解决问题的尝试,所以我在下面包含了一些代码,展示了一种方法:

# Load libraries
library(tidyverse)
library(RColorBrewer)

# Create a dataframe called "df"
df <- data.frame("ID" = c("Householders under 25", "Householders 25-44",
                          "Householders 45-64", "Householders 65+"),
                 "Median Household Income" = c(21835, 38317, 40424, 23763),
                 "Margin of Error" = c(3695, 1609, 2327, 1592),
                 check.names = FALSE)
df
#>                     ID Median Household Income Margin of Error
#>1 Householders under 25                   21835            3695
#>2    Householders 25-44                   38317            1609
#>3    Householders 45-64                   40424            2327
#>4      Householders 65+                   23763            1592

df %>% 
  # Add some new columns to the dataframe ("Median_minus_MOE" / "Median_plus_MOE") and convert ID to an ordered factor
  mutate("Median_minus_MOE" = `Median Household Income` - `Margin of Error`,
         "Median_plus_MOE" = `Median Household Income` + `Margin of Error`,
         "ID" = factor(ID, levels = c("Householders under 25",
                                      "Householders 25-44",
                                      "Householders 45-64",
                                      "Householders 65+"),
                       ordered = TRUE)) %>% 
  # Plot the data, using with Income on the y axis and ID on the x axis
  ggplot(aes(y = `Median Household Income`, x = ID, colour = ID)) +
  geom_pointrange(aes(ymin = Median_minus_MOE,
                      ymax = Median_plus_MOE),
                  size = 1) +
  ylab("Income ($)") +
  # Change the colour scheme
  scale_color_brewer(palette = "Set2") +
  # Change the scale to "dollars" and set the 'breaks' for tick marks
  scale_y_continuous(label = scales::dollar_format(),
                     breaks = seq(15000, 45000, 5000)) +
  # Flip the y axis and the x axis (so "Income" is now on the bottom)
  coord_flip(ylim = c(15000, 45000)) +
  # Change the theme
  theme_classic(base_size = 16, base_family = "Helvetica") +
  # Give the plot a title
  ggtitle("Median Household Income ± Margin of Error in Newark, NJ") +
  # Remove the legend and y axis line/ticks/title
  theme(legend.position = "none",
        axis.line.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank())

示例_1.png


推荐阅读