DanielMc
DanielMc

Reputation: 139

ggplot geom_line - setting colour of lines doesn't work?

I'm trying to plot several lines and then colouring them grey. However, whatever the colour I set, I get black lines. And if I put colour inside the aesthetic, then I get different colours (as expected), even if I specify the argument colour again outside aes().

I'm sure I'm missing something very basic here!

library(tidyverse)
library(ggplot)

country <- c(rep("A", 10), rep("B",10), rep("C", 10))
year <- c(2000:2009, 2000:2009, 2000:2009)
value <- c(rnorm(10), rnorm(10, mean = 0.5), rnorm(10, mean = 1.1))

myData <- tibble(country, year, value) %>%
mutate(avg = mean(value))
ggplot(myData, 
       aes(x = year, y = value, country = country), 
       colour = "grey") +
geom_line()

Upvotes: 2

Views: 1706

Answers (2)

TarJae
TarJae

Reputation: 78917

Here is an othe approach: How you can use scale_color_manual:

p <- ggplot(myData, aes(x = year, y = value, color=country)) +
  geom_line() 

p + scale_color_manual(values=c("#a6a6a6", "#a6a6a6", "#a6a6a6"))

Instead of using hex color you could also use: p + scale_color_manual(values=c("gray69", "gray69", "gray69"))

enter image description here

Upvotes: 0

G. Grothendieck
G. Grothendieck

Reputation: 269441

Try this:

ggplot(myData, aes(x = year, y = value, country = country, colour = I("grey"))) +
geom_line()

screenshot

Upvotes: 3

Related Questions