Ggplot: Colour Points by Groups Based on User Defined Colours

Custom colors for groups using ggplot2

You need to map color to Species variable then use scale_color_manual (not fill)

require(MASS)
require(ggplot2)

data("iris")
my.data <- iris
model <- lda(formula = Species ~ ., data = my.data)
data.lda.values <- predict(model)
plot.data <- data.frame(X = data.lda.values$x[, 1], Y = data.lda.values$x[, 2], Species = my.data$Species)

my_colors <- c("yellow", "magenta", "cyan")
p <- ggplot(data = plot.data, aes(x = X, y = Y, color = Species)) +
geom_point() +
scale_color_manual(values = my_colors) +
theme_bw()
p

Sample Image

Probably better to use Set2 (colorblind safe, print friendly) from ColorBrewer

p <- ggplot(data = plot.data, aes(x = X, y = Y, color = Species)) +
geom_point() +
scale_color_brewer(palette = "Set2") +
theme_bw()
p

Sample Image

Created on 2019-03-10 by the reprex package (v0.2.1.9000)

Assign points colours according both x and y values

Try this:

library(dplyr)
library(ggplot2)

my_data <- iris %>%
mutate(width_length = paste0(cut(Petal.Width, c(0, 0.75, 2.25, Inf), right=FALSE), ' _ ',
cut(Petal.Length, c(0, 2, 4, Inf), right=FALSE)))
ggplot(my_data) +
geom_point(aes(x = Petal.Width,
y = Petal.Length,
color = width_length))

Output:
Sample Image

ggplot color is not automatically coloring based on group

@teunbrand was spot on.
Interesting. You may have somehow more or less voluntarily used I(), which lets R interpret an object "as is". See also ?I

Here how to convert back to plain character:

You can do that either temporarily in the call to ggplot itself, or more permanently, by assignment (which I think you want to do).

update
in the comments, user teunbrand pointed to the S3 Method scale_type.AsIs, which is why using an "asIs" object works just like using scale...identity

## this is to reproduce your data structure
iris2 <- iris
iris2$Species <- I(as.character(iris2$Species))

library(ggplot2)
ggplot(iris2, aes(x=Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point()
#> Error: Unknown colour name: setosa

#convert withing ggplot
ggplot(iris2, aes(x=Sepal.Length, y = Sepal.Width, color = as.character(Species))) +
geom_point()

Sample Image

## convert by assignment
iris2$Species <- as.character(iris2$Species)

ggplot(iris2, aes(x=Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point()

Sample Image

Created on 2020-07-01 by the reprex package (v0.3.0)

Color ggplot points based on defined color codes

The line you claimed didn't work:

df3$color.code.new <- ifelse(is.na(df3$color.codes), "#000000", df3$color.codes)

works for me, although I think you missed a digit in one of your hex colors. Once you have that straightened out, you just want to use scale_colour_manual with something like this:

tick  <- c("GE","APPL","GM","BTU","WMT","JPM","LUV")
price <- c(22,900,20,22,80,31,35)
volume<- c(300,500,100,107,400,300,325)
df1 <- data.frame(ticker=tick, price=price, volume=volume)

tick<-c("GE","APPL","GM","BTU","WMT")
ccodes<-c("#3399FF", "#FF0000", "#CC00FF", "#993300", "#66CC00")
cnames<-c("blue", "red", "purple", "brown", "green")
df2=data.frame(ticker=tick, color.codes=ccodes, color.names=cnames)

## merge color specifcations into data
df3 <-merge(df1,df2, by=("ticker"), all.x=TRUE, all.y=TRUE)
df3$color.code.new <- ifelse(is.na(df3$color.codes), "#000000", df3$color.codes)

p <- ggplot(df3, aes(volume, price,colour = ticker))+
geom_point()
p + scale_colour_manual(breaks = df3$ticker,values = df3$color.code.new)

Sample Image

ggplot: How to display multiple groups via color and shape with point and line

Based on further information in comments from the OP, we are looking for something like this:

ggplot(data, aes(x=year, y=variable, col=factor(id1))) + 
geom_line() +
geom_point(aes(shape=factor(id2), size = factor(id2))) +
labs(shape = "group 2", colour = "group 1", size = "group 2")

Sample Image

Color points in overlayed scatterplots in ggplot R

The best way of doing is to reshape your dataframe in a longer format (here I'm using the pivot_longer function from tidyr package):

library(tidyr)
library(dplyr)
df %>% pivot_longer(.,- groups, names_to = "var", values_to = "val")

# A tibble: 15 x 3
groups var val
<chr> <chr> <dbl>
1 a grade1 510
2 a grade2 520
3 a grade3 530
4 b grade1 405
5 b grade2 430
6 b grade3 410
7 c grade1 308
8 c grade2 300
9 c grade3 320
10 d grade1 201
11 d grade2 190
12 d grade3 210
13 e grade1 99
14 e grade2 110
15 e grade3 105

And then to get your graph, you can simply do:

library(dplyr)
library(ggplot2)
library(tidyr)
df %>% pivot_longer(.,- groups, names_to = "var", values_to = "val") %>%
ggplot(aes(x= groups, y = val, color = var))+
geom_point()

Sample Image

You can control the pattern of color used by using scale_color_manual function



Related Topics



Leave a reply



Submit