r - 为什么我的堆叠 geom_bar 没有绘图?我对单元格的编辑正在产生某种错误
问题描述
提前感谢您的任何帮助或意见。
当我尝试绘制已编辑的数据框时,出现此错误:“abs(x) 中的错误:数学函数的非数字参数”。
这是我想要做的,我不知道错误来自哪里:
- 将年龄增量分隔到单独的列中
- 用 + 删除所有最终条目(或理想情况下将它们全部更改为 ... 到 100)
- 计算每个增量的范围
- 在水平堆积条形图中绘制各种增量
这是代码。:
require(tidyr)
require(dplyr)
require(ggplot2)
CoRo_age <- CoRo_age %>%
separate(age_cat, c("Increment 1", "Increment 2", "Increment 3", "Increment 4", "Increment 5",
"Increment 6", "Increment 7", "Increment 8", "Increment 9", "Increment 10", "Increment 11",
"Increment 12","Increment 13", "Increment 14", "Increment 15", "Increment 16","Increment 17"), ",")
CoRo_age$'Increment 2'[!grepl("-",CoRo_age$'Increment 2')] <- ""
CoRo_age$'Increment 3'[!grepl("-",CoRo_age$'Increment 3')] <- ""
CoRo_age$'Increment 4'[!grepl("-",CoRo_age$'Increment 4')] <- ""
CoRo_age$'Increment 5'[!grepl("-",CoRo_age$'Increment 5')] <- ""
CoRo_age$'Increment 6'[!grepl("-",CoRo_age$'Increment 6')] <- ""
CoRo_age$'Increment 7'[!grepl("-",CoRo_age$'Increment 7')] <- ""
CoRo_age$'Increment 8'[!grepl("-",CoRo_age$'Increment 8')] <- ""
CoRo_age$'Increment 9'[!grepl("-",CoRo_age$'Increment 9')] <- ""
CoRo_age$'Increment 10'[!grepl("-",CoRo_age$'Increment 10')] <- ""
CoRo_age$'Increment 11'[!grepl("-",CoRo_age$'Increment 11')] <- ""
CoRo_age$'Increment 12'[!grepl("-",CoRo_age$'Increment 12')] <- ""
CoRo_age$'Increment 13'[!grepl("-",CoRo_age$'Increment 13')] <- ""
CoRo_age$'Increment 14'[!grepl("-",CoRo_age$'Increment 14')] <- ""
CoRo_age$'Increment 15'[!grepl("-",CoRo_age$'Increment 15')] <- ""
CoRo_age$'Increment 16'[!grepl("-",CoRo_age$'Increment 16')] <- ""
CoRo_age$'Increment 17'[!grepl("-",CoRo_age$'Increment 17')] <- ""
CoRo_diff <- apply(CoRo_age[1:50,2:17], c(1,2), function(x) abs(as.numeric(unlist(eval(parse(text=x))))))
CoRo_age <- as.data.frame(cbind(CoRo_age[,1], CoRo_diff))
colnames(CoRo_age)[1] <- c("States")
CoRo_age %>%
gather("Increments", "Range", -States) %>%
ggplot(aes(x=States, y=Range)) +
geom_bar(stat="identity", fill=Increments) +
coord_flip()
这是数据帧的 dput 输出:
CoRo_age <- structure(list(state = c("Minnesota", "New York", "North Carolina",
"South Carolina", "Delaware", "Illinois", "Louisiana", "Georgia",
"Arkansas", "Connecticut", "Idaho", "Maryland", "Massachusetts",
"Tennessee", "Michigan", "California", "Virginia", "District of Columbia",
"Kentucky", "Arizona", "Maine", "Colorado", "Hawaii", "Indiana",
"Iowa", "Kansas", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "Texas",
"Rhode Island", "South Dakota", "Alabama", "Alaska", "Oklahoma",
"Oregon", "Pennsylvania", "Utah", "Wisconsin", "West Virginia",
"Vermont", "Wyoming", "Washington", "North Dakota", "Florida"
), age_cat = c("0-5,6-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,100+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90+", "0-17,18-24,25-49,50-64,65-74,75+",
"11-20,21-30,31-40,41-50,51-60,61-70,71-80,81+", "0-4,5-17,18-34,35-49,50-64,65+",
"0-20,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-5,6-17,18-29,30-39,40-49,50-59,60-69,70+",
"0-1,01-04,05-09,10-17,18-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-17,18-24,25-44,45-64,65+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-18,18-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,100+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-10,11-20,21-30,41-50,51-60,61-70,71-80,80+", "0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-17,18-49,50-64,65+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-4,5-14,15-19,20-24,25-34,35-44,45-54,55-64,65-74,75+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-20,20-44,45-54,55-64,65,+", "0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-19,20-39,40-59,60+",
"0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-17,18-40,41-60,61-80,80+",
"0-9,10-17,18-24,25-34,35-44,45-54,55-64,65-74,75-84,85+", "0-18,18-29,30-39,40-49,50-59,60-69,70-79,80-89,90+",
"0-9,10-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-84,85-90,90+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-19,20-34,35-44,45-54,55-64,65-74,75-84,85+",
"0-10,10-19,20-29,30-39,40-49,50-59,60-69,70+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-4,5-17,18-29,30-49,50-64,65-79,80+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90+",
"0-1,1-9,10-19,20-29,30-39,40-49,50-59,60-64,65-69,70-74,75-79,80+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,100+",
"0-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-4,5-24,25-49,50-64,65+,",
"0-10,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-4,5-17,18-35,36-49,50-64,65+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90-99,>100",
"1-14,15-24,25-44,45-64,65-84,85+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80-89,90+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70+", "0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+",
"0-18,19-29,30-39,40-49,50-59,60-69,70-79,80+", "00-19,20-39,40-59,60-79,80+",
"0-9,10-19,20-29,30-39,40-49,50-59,60-69,70-79,80+", "0-4,5-14,15-24,25-34,35-44,45-54,55-64,65-74,75-84,85+"
)), row.names = c(1L, 3L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L,
21L, 24L, 25L, 28L, 36L, 39L, 40L, 41L, 55L, 56L, 57L, 58L, 59L,
60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 75L, 76L, 81L, 82L, 83L, 86L, 87L, 88L, 97L, 98L, 101L,
104L, 126L), class = "data.frame")
解决方案
我不确定最终情节的启发性如何,但您可以通过这个完全可重现的示例获得它:
library(ggplot2)
url <- "https://raw.githubusercontent.com/HopeMuller/CoRo/master/State_Ages.csv"
CoRo_age <- read.csv(url)[-1]
ranges <- lapply(CoRo_age$age_cat, function(x)
sapply(strsplit(gsub("^(.*)\\+$", "\\1-100",
gsub("\\+,$", "-100",
gsub("^(.+),+\\+$", "\\1-100",
gsub("(100\\+)|(>100)", "100-101",x)))), ",")[[1]],
function(x) {
sapply(strsplit(x, "-"), function(x) diff(as.numeric(x)) + 1)
}))
df <- do.call(rbind,
mapply(function(x, y) {
data.frame(State = x, range = factor(seq_along(y)), Years = y)
}, CoRo_age$state, ranges, SIMPLIFY = FALSE))
ggplot(df, aes(State, Years, fill = range)) +
geom_col(color = "black", width = 1) +
coord_flip() +
theme_bw() +
theme(legend.position = "none")
推荐阅读
- javascript - 如何使用 javascript 正确发布 x-www-form-urlencoded 数据?
- blockchain - 需要从任何区块链上的块中检索交易
- sql-server - 如何在 MSSQL 中查找两个表之间的差异
- json - Webscraper,将列表提取到.json?
- android - 从 Kotlin Generic 获取泛型类
- linux - 如何从多个文件中打印每第 n 行并将其写入第 n 行的每一组?
- email - 安装postfixadmin?
- api - 无法使用 Google Tag Manager API 在 Google Tag Manager 中创建标签
- javascript - 从 AJAX 获取原始 HTTP 响应
- linux - Nginx - Lua 无法保存图像文件