Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

r - How to use ggplot to colour by a heading with "+" or "NA" values for each?

I have

ggplot(data = file, mapping = aes(x = x_category,y = y_category))+geom_point(aes((color = sig))

where data = file is an imported .csv, where x_category and y_category are columns from that file.

and the color for "sig" comes from a column filled with "+"s or blank cells.

How do I specify the colour for "+" vs blank cell. plot

Secondly, how would one extend this to "colour the spot yellow if the string (from a different column instead, of "+" and NA) contains the word "banana".

EDIT:


cortex <-df

names(df)

names(df)[1] <- "y_category"
names(df)[3] <- "x_category"

df <- data.frame (
  x_category = rnorm(5),
  y_category = rnorm(5),
  sig = c("+","+", NA, NA,"+"),
)

ggplot(df,aes(x = x_category, y = y_category))+geom_point(aes(color = sig) ) +
  scale_colour_manual(values = c("+" = "dodgerblue"),
                      na.value = "tomato")

results in the error:

Error in data.frame(x_category = rnorm(5), y_category = rnorm(5), sig = c("+",  : 
      argument is missing, with no default

Thank you - regarding the banana example, I actually meant the following: Imagine one has a column containing cells which all contain lots of different words (too many to list), is there a way to ask "is banana mentioned in these words?" If yes = make point yellow.

question from:https://stackoverflow.com/questions/65830679/how-to-use-ggplot-to-colour-by-a-heading-with-or-na-values-for-each

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...