r - 如何根据提供的数据在 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)
解决方案
这是一个面向专业程序员的网站,因此您不太可能在这里获得所需的帮助。如果您在作业问题上遇到问题,最好的办法是询问您的课程主管/讲师/导师/等,他们可以帮助您解决问题。
因为您已经发布了自己解决问题的尝试,所以我在下面包含了一些代码,展示了一种方法:
# 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())
推荐阅读
- javascript - Javascript:选择 2 个选项以同时显示测量类型和时间间隔
- python - 将绘图图形放入传单
- javascript - 仅将唯一值推送到数组中
- javascript - Discord.js setStatus() 和 setPresence() 不起作用
- intellij-idea - 在 IntelliJ 中跳转到实现而不是声明
- javascript - 显示保存在 useRef 变量中的值
- r - 根据包含在闪亮仪表板中的对象调整框高度
- javascript - React Native Animated - onScroll 性能问题
- jenkins - 如何通过 api 或 cli 从 jenkins 服务器获取建议的 jenkins 插件列表?
- javascript - 未捕获的 ReferenceError:无法在 html 表单中调用 ajax 函数