我有如下数据:
Age New_York Boston Chicago
1 under 1 994 969 1011
2 1 991 1094 1282
3 2 991 1369 1274
4 3 818 1051 1098
5 4 902 1012 1308
6 5 866 1077 1040
7 6 826 1684 929
8 7 793 1071 1077
9 8 714 1387 984
10 9 890 855 749
这是产生我的数据的代码:
structure(list(Age = c("under 1", "1", "2", "3", "4", "5", "6",
"7", "8", "9"), New_York = c("994", "991", "991", "818", "902",
"866", "826", "793", "714", "890"), Boston = c("969", "1094",
"1369", "1051", "1012", "1077", "1684", "1071", "1387", "855"
), Chicago = c("1011", "1282", "1274", "1098", "1308", "1040",
"929", "1077", "984", "749")), .Names = c("Age", "New_York",
"Boston", "Chicago"), row.names = c(NA, 10L), class = "data.frame")
我正在尝试在R中绘制一个海盗图,以年龄作为IV,并给出不同城市的答案。我想知道在R中执行此操作的代码是什么
答案 0 :(得分:0)
在您的数据中,看起来您在城市下的值是字符。您需要确保它们首先是数字。然后,您需要将数据从宽转换为长,然后可以简单地使用geom_pirate
包中的ggpirate
:
install.packages('devtools')
devtools::install_github("mikabr/ggpirate")
library(ggpirate)
df_long <- gather(df_wide[,c(2:4)], key = "City", value = "Value")
ggplot(df_long, aes(City, Value)) +
geom_pirate(aes(col = City), show.legend = TRUE)
请注意,我为您的原始数据集df_wide
命名,并将其中的char
更改为num
。
编辑:或者,您可以使用pirateplot()
包中的yarrr
函数:
library(yarrr)
df <- structure(list(Age = c("under 1", "1", "2", "3", "4", "5", "6",
"7", "8", "9"), New_York = c("994", "991", "991", "818", "902",
"866", "826", "793", "714", "890"), Boston = c("969", "1094",
"1369", "1051", "1012", "1077", "1684", "1071", "1387", "855"
), Chicago = c("1011", "1282", "1274", "1098", "1308", "1040",
"929", "1077", "984", "749")), .Names = c("Age", "New_York",
"Boston", "Chicago"), row.names = c(NA, 10L), class = "data.frame")
df$New_York <- as.numeric(df$New_York)
df$Boston <- as.numeric(df$Boston)
df$Chicago <- as.numeric(df$Chicago)
df_long <- gather(df[,c(2:4)], key = "City", value = "Value")
pirateplot(formula = Value ~ City,
data = df_long)