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
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
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:
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()
## convert by assignment
iris2$Species <- as.character(iris2$Species)
ggplot(iris2, aes(x=Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point()
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)
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")
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()
You can control the pattern of color used by using scale_color_manual
function
Related Topics
An Na in Subsetting a Data.Frame Does Something Unexpected
Read CSV File in R with Currency Column as Numeric
Splitting a Data Frame into Equal Parts
Return Df with a Columns Values That Occur More Than Once
Replace Missing Values (Na) in One Data Set with Values from Another Where Columns Match
Set the Order of a Stacked Bar Chart by the Value of One of the Variables
R - How to Make Barplot Plot Zeros for Missing Values Over the Data Range
Plotting Multiple Time Series on the Same Plot Using Ggplot()
Sum of Rows Based on Column Value
Left-Adjust Title in Ggplot2, or Absolute Position for Ggtitle
Add Values to a Reactive Table in Shiny
Specifying Ggplot2 Panel Width
What Type of Graph Is This? and Can It Be Created Using Ggplot2
Getting All Combinations Which Sum Up to 100 Using R
Two-Way Density Plot Combined with One Way Density Plot with Selected Regions in R