You can use scale_colour_manual()
. Because ""
(empty string) is not a valid name, you can define colours only by the "+"
name. It will still give empty strings a colour, it's just you cannot colour it by naming the values
argument.
library(ggplot2)
df <- data.frame(
x_category = LETTERS[1:5],
y_category = letters[1:5],
pos = c("+", "+", "", "", "+")
)
ggplot(df, aes(x_category, y_category)) +
geom_point(aes(colour = pos)) +
scale_colour_manual(values = c("+" = "dodgerblue", "tomato"))
Created on 2021-01-21 by the reprex package (v0.3.0)
Based on your updated information, I can make my updated answer the following. Seeing as the .csv file is read in as a data.frame in most cases, I don't think that makes a difference for how your question should be solved. I'm just using dummy data since I haven't seen a sample of yours.
library(ggplot2)
df <- data.frame(
x_category = rnorm(5),
y_category = rnorm(5),
sig = c("+", "+", NA, NA, "+"),
fruits = c("apple", "banana", "orange", "strawberry", "pear")
)
ggplot(df, aes(x_category, y_category)) +
geom_point(aes(
colour = ifelse(fruits == "banana", "banana", sig)
)) +
scale_colour_manual(values = c("+" = "dodgerblue", "banana" = "yellow"),
na.value = "tomato")
Created on 2021-01-21 by the reprex package (v0.3.0)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…