ggplot2中点的手动颜色

时间:2017-05-11 11:09:47

标签: r ggplot2

我有一个因子图,我想控制为图中每个线/点选择的颜色。我检查了在线资源,并且看到我应该使用ggplot2中的+scale_color_manual()函数。

虽然这会创建所需的输出,但它还会创建一个我不希望拥有的额外图例: enter image description here

在没有创建额外图例的情况下,实现手动控制线路的正确方法是什么?

代码:

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

2 个答案:

答案 0 :(得分:2)

如果您想要合并两个图例,只需删除txt1scale_shape_discrete()

scale_linetype_discrete()

enter image description here

答案 1 :(得分:1)

添加+ theme(legend.position="none")以删除所有图例:

enter image description here

如果您只想删除第二个图例,请使用scale_color_manual(values = c("#999999", "#E69F00"), guide=FALSE)

enter image description here