我已经根据我的数据创建了 ggplot(下面的示例):
我创建了 NKV
fiddle 图,并在其上绘制了各个 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
部分实现不同的颜色?
什么原因导致错误?
谢谢!
最佳答案
如果您的PID
级别多于“Spectral”调色板的颜色,您可以尝试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")
关于r - 更改 ggplot 中的调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500035/