簡體   English   中英

ggplot2中點的手動顏色

[英]Manual colour for points in ggplot2

我有一個因子圖,我想控制為圖中每個線/點選擇的顏色。 我已經檢查了在線資源,並且看到我應該在+scale_color_manual()使用+scale_color_manual()函數。

雖然這會創建所需的輸出,但它也會創建一個我不希望擁有的額外圖例: 在此輸入圖像描述

在沒有創建額外傳奇的情況下,實現手動控制線路的正確方法是什么?

碼:

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

如果要組合兩個圖例,只需刪除scale_shape_discrete()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) 

在此輸入圖像描述

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

在此輸入圖像描述

如果您只想刪除第二個圖例,請使用scale_color_manual(values = c("#999999", "#E69F00"), guide=FALSE)

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM