2017-04-19 155 views
2

我从我的数据(下面的示例)创建ggplot。我想区分哪些数据点属于哪个PID。到目前为止好:改变颜色的调色板中ggplot

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

我认为brewer颜色只适用于离散变量。看看'scale_colour_gradient'和'scale_colour_gradient2'。 –

回答

2

如果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") 
+0

谢谢@ mt1022。转换'PID'就是我所缺少的。现在'scale_color_brewer(palette =“Spectral”)'就像一个魅力! – Danka