Nneka
Nneka

Reputation: 1850

changing the color palette in ggplot

I have created ggplot from my data (sample below):

I have created a violin plot of the NKV with the individual NKV data points plotted over it. I want to differentiate betweeen which PID my datapoints belong to. 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. 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?

What causes the error?

Thanks!

Upvotes: 2

Views: 4611

Answers (1)

mt1022
mt1022

Reputation: 17289

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:

# 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. 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")

Upvotes: 3

Related Questions