假设我从假设调查中收集了以下数据集:
name age homeowner favorite_color pets
Bill 45 Yes Blue (cat, dog, fish)
Mary 33 Yes Red (cat, dog)
Joe 55 Yes Blue (cat, bird, fish)
Sue 38 No Green (fish, bird)
每个人都能够对他们拥有的宠物类型提供多种回应。
是否有一种简单的方法可以使用ggplot2
创建以下散点图?
x axis = homeowner
y axis = favorite_color
col = pets
基本上,我希望绘制三个分类值。我无法弄清楚如何最好地提取宠物的嵌套矢量数据。为了简单起见,我们假设他们只允许携带各种宠物。
在(是的,蓝色)的交叉点,我希望看到一个抖动的情节:
您可以在这里提供的任何帮助都非常感谢 - 这是r。
的新功能答案 0 :(得分:0)
survey <- data.frame(name = c("Bill", "Mary", "Joe", "Sue"),
age = c(45, 33, 55, 38),
homeowner = c(rep("Yes", times = 3), "No"),
favorite_color = c("Blue", "Red", "Blue", "Green"),
pets = c("(cat, dog, fish)",
"(cat, dog)",
"(cat, bird, fish)",
"(fish, bird)"))
# Rebuild your data
all_pets <- c("cat", "dog", "fish", "bird")
# Specify all kinds of pets you have (Someone else may have a better way here)
name <- NULL
pets <- NULL
for (i in 1:nrow(survey)) {
for (j in 1:length(all_pets)) {
if (grepl(all_pets[j], survey$pets[i])) {
name <- append(name, as.character(survey$name[i]))
pets <- append(pets, all_pets[j])
}
}
}
new_survey <- data.frame(name, pets)
merged_survey <- merge(survey, new_survey, by = "name")
现在merged_survey应该拥有您需要的信息。现在我们可以用ggplot2绘制它。
require(ggplot2)
g <- ggplot(aes(x = homeowner, y = favorite_color), data = merged_survey)
g + geom_point(aes(color = pets.y), position = position_jitter(0.1, 0.1))
position_jitter函数每次都会随机抖动这些点,因此您可能无法在与我完全相同的位置看到这些点。您可以通过更改position_jitter中的数字来调整抖动宽度和高度。所有标签都可以在以后更改,但这可能是偏离主题的。