我从我的数据中创建了ggplot(下面的示例):
我创建了NKV
的小提琴图,其上绘制了单独的NKV
个数据点。我想区分PID
我的数据点所属的区域。到目前为止一切顺利:
violin.murgang <- ggplot(nkv.murgang, aes(x = factor("Murgang"), nkv.murgang$NK)) +
geom_violin(color = "black", fill = "darkorange") +
ggtitle("NKV Murgang - Einfamilienhaus") +
labs(x = "Prozess", y = "Nutzen / Konsten \n Verhälhniss") +
stat_summary(geom = "text", fun.y = quantile,
aes(label=sprintf("%1.1f", ..y..)),
position=position_nudge(x=0.4), size=3) +
theme (legend.position = "none") +
stat_summary(fun.data = give.n, geom = "text", position=position_nudge(x=-0.4)) +
geom_jitter(aes(col = PID ), width = 0.35)
violin.murgang
问题是所有NKV
数据点仅以不同的蓝色阴影显示。我想要有不同的颜色。我试过添加这个:
scale_colour_brewer(palette="Spectral")
产生错误:
Error: Continuous value supplied to discrete scale
如何为geom_jitter
部分实现不同的颜色?
导致错误的原因是什么?
谢谢!
答案 0 :(得分:3)
如果PID
的级别高于“光谱”的颜色级别。调色板,你可以尝试scale_color_distiller
,它将啤酒颜色扩展到连续比例,请参阅scale_color_distiller
的手册:
# Use distiller variant with continous data
v <- ggplot(faithfuld) +
geom_tile(aes(waiting, eruptions, fill = density))
v
v + scale_fill_distiller()
v + scale_fill_distiller(palette = "Spectral")
因此,我们可以尝试:
ggplot(nkv.murgang, aes(x = factor("Murgang"), nkv.murgang$NK)) +
geom_violin(color = "black", fill = "darkorange") +
ggtitle("NKV Murgang - Einfamilienhaus") +
labs(x = "Prozess", y = "Nutzen / Konsten \n Verhälhniss") +
stat_summary(geom = "text", fun.y = quantile,
aes(label=sprintf("%1.1f", ..y..)),
position=position_nudge(x=0.4), size=3) +
theme (legend.position = "none") +
geom_jitter(aes(color = PID), width = 0.35) +
scale_color_distiller(palette = "Spectral")
如果数据有几个级别,我们可以使用离散比例。 PID
是整数,可以使用离散比例。您应该首先将其转换为字符或因子:
ggplot(nkv.murgang, aes(x = factor("Murgang"), nkv.murgang$NK)) +
geom_violin(color = "black", fill = "darkorange") +
ggtitle("NKV Murgang - Einfamilienhaus") +
labs(x = "Prozess", y = "Nutzen / Konsten \n Verhälhniss") +
stat_summary(geom = "text", fun.y = quantile,
aes(label=sprintf("%1.1f", ..y..)),
position=position_nudge(x=0.4), size=3) +
theme (legend.position = "none") +
geom_jitter(aes(color = as.factor(PID) ), width = 0.35) +
scale_color_brewer(palette = "Spectral")