JimGrange
JimGrange

Reputation: 272

Manual colour for points in ggplot2

I have a factorial graph and I would like to have control over the colours chosen for each line/point in the graph. I have checked online resources, and have seen I should use the +scale_color_manual() function within ggplot2.

Although this creates the desired output, it also creates an extra legend that I do not wish to have: enter image description here

What is the correct way of achieving the manual control over lines without creating the extra legend, please?

Code:

# load library
library(ggplot2)

# intialise random seed for reproducibility
set.seed(42)

# generate fictitous averaged data
age <- gl(2, 4, labels = c("Younger", "Older"))
sequence <- gl(2, 2, 8, labels = c("ABA", "CBA"))
response <- gl(2, 1, length = 8, labels = c("Repetition", "Switch"))
accuracy <- runif(length(age), min = 0.90, max = 1)
se <- runif(length(age), min = 0.002, max = 0.008)

# collate into data frame
data <- data.frame(age, sequence, response, accuracy, se)


# do plot
pd <- position_dodge(0.08)

plot <- ggplot(data, aes(x = sequence, y = accuracy, group = response, 
                        colour = response))
plot <- plot + geom_errorbar(aes(ymin = accuracy - se, ymax = accuracy + se), 
                             width = .15, size = 0.5, position = pd)
plot <- plot + geom_line(aes(linetype = response), position = pd)
plot <- plot + geom_point(aes(shape = response), size = 2.3, position = pd)
plot <- plot + scale_x_discrete(name = "Task Sequence") + 
  scale_y_continuous(name = "Accuracy (Proportion)")
plot <- plot + scale_shape_discrete(name = "Response") + 
  scale_linetype_discrete(name = "Response")
plot <- plot + facet_grid(  ~ age) 
plot + scale_color_manual(values = c("#999999", "#E69F00"))

Upvotes: 2

Views: 2194

Answers (2)

Stewart Macdonald
Stewart Macdonald

Reputation: 2132

Add + theme(legend.position="none") to remove all legends:

enter image description here

Use scale_color_manual(values = c("#999999", "#E69F00"), guide=FALSE) if you want to remove just the second legend.

enter image description here

Upvotes: 1

erc
erc

Reputation: 10123

If you want to combine the two legends you can simply delete scale_shape_discrete() and scale_linetype_discrete()

ggplot(data, aes(x = sequence, y = accuracy, group = response, 
             colour = response)) +
        geom_errorbar(aes(ymin = accuracy - se, ymax = accuracy + se), 
                      width = .15, size = 0.5, position = pd) +
        geom_line(aes(linetype = response), position = pd) +
        geom_point(aes(shape = response), size = 2.3, position = pd) +
        scale_x_discrete(name = "Task Sequence") + 
        scale_y_continuous(name = "Accuracy (Proportion)") +
        scale_color_manual(values = c("#999999", "#E69F00")) +
        facet_grid(~ age) 

enter image description here

Upvotes: 4

Related Questions