我对数据进行了分组,希望将其绘制成一组散点图。这是数据
df <- data.frame(id = rep(1:46, each = 3),
bevType = rep(c("water","decaf","coffee"), times = 46),
score = c(2.9,1.0,0.0,9.5,5.0,4.5,9.0,3.0,5.0,5.0,0.0,3.0,9.5,2.0,3.0,8.5,0.0,6.0,5.2,3.0,4.0,8.4,7.0,2.0,10.0,0.0,3.0,7.3,1.0,1.8,8.5,2.0,9.0,10.0,5.0,10.0,8.3,2.0,5.0,6.0,0.0,5.0,6.0,0.0,5.0,10.0,0.0,5.0,6.8,1.0,4.8,8.0,1.0,4.0,7.0,4.0,6.0,6.5,1.0,3.1,9.0,1.0,0.0,6.0,0.0,2.0,9.5,4.0,6.0,8.0,1.0,3.8,0.4,0.0,7.0,7.0,0.0,3.0,9.0,2.0,5.0,9.5,2.0,7.0,7.9,5.0,4.9,8.0,1.0,1.0,9.3,5.0,7.9,6.5,2.0,3.0,8.0,2.0,6.0,10.0,0.0,5.0,6.0,0.0,5.0,6.8,0.1,7.0,8.0,3.0,9.1,8.2,0.0,7.9,8.2,5.0,0.0,9.2,1.0,3.1,9.1,3.0,0.6,5.7,2.0,5.1,7.0,0.0,7.4,8.0,1.0,1.5,9.1,4.0,4.3,8.5,8.0,5.0))
现在,为了获取每个组中的点以将它们很好地分成几列,我们必须在position = position_dodge()
函数内指定一个geom_jitter
参数
ggplot(df, aes(x = bevType, colour = bevType)) +
geom_jitter(aes(y = score), shape = 1, position = position_dodge(width=0.9)) +
scale_colour_manual(name = "Beverage Type", labels = c("Water", "Decaf", "Coffee"), values = c("#616a6b", "#00AFBB", "#E7B800")) +
labs(y = "Score", x = "Beverage Type") +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.title = element_blank())
但是如果我添加一个width =
参数来使点横向抖动,
ggplot(df, aes(x = bevType, y = score, colour = bevType)) +
geom_jitter(shape = 1, position = position_dodge(width=0.9), width = 0.1) +
scale_colour_manual(name = "Beverage Type", labels = c("Water", "Decaf", "Coffee"), values = c("#616a6b", "#00AFBB", "#E7B800")) +
labs(y = "Score", x = "Beverage Type") +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.title = element_blank())
但是我得到了错误
Error: Specify either `position` or `width`/`height`
因此,我在width=
函数中收集了两个geom_jitter()
参数,这引起了混淆,所以如何水平抖动每列中的点?
答案 0 :(得分:1)
我认为您不需要position_dodge()
。只需在geom_jitter()
中指定适当的宽度即可。例如:
ggplot(df1, aes(bevType, score, colour = bevType)) +
geom_jitter(shape = 1, width = 0.2) +
scale_colour_manual(name = "Beverage Type",
labels = c("Water", "Decaf", "Coffee"),
values = c("#616a6b", "#00AFBB", "#E7B800")) +
labs(y = "Score",
x = "Beverage Type") +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.title = element_blank())