[英]changing the color palette in ggplot
I have created ggplot from my data (sample below): 我已经根据数据创建了ggplot(以下示例):
I have created a violin plot of the NKV
with the individual NKV
data points plotted over it. 我创建了
NKV
的小提琴图,并在其上绘制了各个NKV
数据点。 I want to differentiate betweeen which PID
my datapoints belong to. 我想区分我的数据点属于哪个
PID
。 So far so good: 到现在为止还挺好:
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
The problem is that all the NKV
data points are only visualized in different shade of blue. 问题在于,所有
NKV
数据点只能以不同的蓝色阴影显示。 I would like to have different colours. 我想有不同的颜色。 I have tried adding this:
我尝试添加以下内容:
scale_colour_brewer(palette="Spectral")
which yields the error: 产生错误:
Error: Continuous value supplied to discrete scale
How can i achieve having different colour for the geom_jitter
part? 如何实现
geom_jitter
部分具有不同的颜色?
What causes the error? 是什么导致错误?
Thanks! 谢谢!
If you PID
have more levels than colors of 'Spectral' palette, you could try scale_color_distiller
, which extends brewer colors to continuous scale, see the manual of scale_color_distiller
: 如果您的
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")
Therefore, we could try: 因此,我们可以尝试:
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")
If you data has a few levels, we could use discrete scales. 如果您的数据有几个等级,我们可以使用离散等级。
PID
is integer, which does work with discrete scales. PID
是整数,可用于离散比例。 You should convert it to character or factor first: 您应该先将其转换为字符或因子:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.