As you want to supply color names to argument colour=
and display also a legend for this argument, you should add scale_colour_identity()
to your last line in function. This scale ensures that values supplied will be interpreted as actual color values. Adding of argument breaks=cols_hex
in function scale()
will ensure ordering of names in legend.
ggplot(NULL) + geom_point(data=data, aes(x=x, y=y, colour=cols_hex), size=size, alpha=alpha) + scale_colour_identity(guide="legend",breaks=cols_hex)

Related Posts:
- Emulate ggplot2 default color palette
- ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”
- ggplot2 line chart gives “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?”
- Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : Polygon edge not found
- Editing legend (text) labels in ggplot
- Increase number of axis ticks
- ggplot2, facet_grid, free scales?
- R Hex to RGB converter
- Error in file(file, “rt”) : cannot open the connection [duplicate]
- t-stat for feature selection
- Could not find function “%<>%” with dplyr loaded
- Interpreting “condition has length > 1” warning from `if` function
- Plotting with ggplot2: “Error: Discrete value supplied to continuous scale” on categorical y-axis
- ggplot2 error : Discrete value supplied to continuous scale
- Error: could not find function “%>%”
- Error in if/while (condition) {: missing Value where TRUE/FALSE needed
- R: Using equation with natural logarithm in nls
- Counting the number of elements with the values of x in a vector
- What does “Error: object ‘
‘ not found” mean? - mean() warning: argument is not numeric or logical: returning NA
- Error in Confusion Matrix : the data and reference factors must have the same number of levels
- Having trouble setting working directory
- How to find the statistical mode?
- Add color to boxplot – “Continuous value supplied to discrete scale” error
- case_when in mutate pipe
- Error in plot.window(…) : need finite ‘xlim’ valuescc
- Logistic Regression on factor: Error in eval(family$initialize) : y values must be 0 <= y <= 1
- Longer object length is not a multiple of shorter object length?
- rmarkdown error “attempt to use zero-length variable name”
- Error in lm.fit(x,y,offset = offset, singular.ok,…) 0 non-NA cases with boxcox formula
- dim(X) must have a positive length when applying function in data frame
- Extract year from date
- Basic – T-Test -> Grouping Factor Must have Exactly 2 Levels
- Persistent invalid graphics state error when using ggplot2
- R – longer object length is not a multiple of shorter object length
- incorrect number of dimensions and incorrect number of subscripts in array
- Rotating and spacing axis labels in ggplot2
- How to debug “contrasts can be applied only to factors with 2 or more levels” error?
- Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘y’, tried every possible way
- R t-test Grouping factor must have exactly 2 levels error
- Understanding the order() function
- Subscript out of bounds – general definition and solution?
- Plot a line graph, error in xy.coords(x, y, xlabel, ylabel, log) : ‘x’ and ‘y’ lengths differ
- Convert a list to a data frame
- Sum rows in data.frame or matrix
- Invalid factor level, NA generated warning
- R: “Unary operator error” from multiline ggplot2 command
- invalid type (list) for variable
- not a Stata version 5-12 .dta file
- How to subset a Data frame column wise using column names?
- How to randomize ggplot factor color scheme, keeping same default distances on color wheel?
- Can’t draw Histogram, ‘x’ must be numeric
- No non-missing arguments warning when using min or max in reshape2
- Having trouble setting working directory
- Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)
- How to move or position a legend in ggplot2
- Singularity in backsolve at level 0, block 1 in LME model
- “installation of package ‘FILE_PATH’ had non-zero exit status” in R
- Nested ifelse statement
- Convert categorical variables to numeric in R
- How do I get RSS from a linear model output
- Warning message: In `…` : invalid factor level, NA generated
- Plot multiple lines (data series) each with unique color in R
- Plot two graphs in same plot in R
- How to overcome error:”attempt to set ‘colnames’ on an object with less than two dimension” in xts object
- How to avoid “Error in stripchart.default(x1, …) : invalid plotting method” error?
- ‘height’ must be a vector or a matrix. barplot error
- What causes an R script to get Killed?
- What’s the difference between facet_wrap() and facet_grid() in ggplot2?
- dplyr mutate with conditional values
- Getting invalid model formula in ExtractVars when using rpart function in R
- R Error (from NA’s to 0): duplicate subscripts for column in Data Frame
- Error in file(filename, “r”, encoding = encoding) : cannot open the connection
- incorrect number of subscripts on matrix in R
- Error in rbind(deparse.level, …) : numbers of columns of arguments do not match R
- Correct syntax for mutate_if
- Plotting data in R; error: default method not implemented for type ‘list’
- Error:attempt to apply non-function
- R apply function with multiple parameters
- Changing fonts in ggplot2
- Error in
: target of assignment expands to non-language object - run a for loop in parallel in R
- Error: Invalid number of ‘breaks’ in R
- Efficiently sum across multiple columns in R
- How to eliminate “NA/NaN/Inf in foreign function call (arg 7)” running predict with randomForest
- Sample random rows in dataframe
- Error in eval(predvars, data, env) : object ‘Rm’ not found
- How to convert data.frame column from Factor to numeric [duplicate]
- Error in x[[i]] : recursive indexing failed at level 2
- Merge error : negative length vectors are not allowed
- non-numeric argument to binary operator
- How to avoid “operations are possible only for numeric, logical or complex types” when computing top 3 elements in each group
- Column standard deviation R [duplicate]
- why nrow(dataframe) and length(dataframe) in r give different results?
- Meaning of objects being masked by the global environment
- Difference between as.POSIXct/as.POSIXlt and strptime for converting character vectors to POSIXct/POSIXlt
- cbind warnings : row names were found from a short variable and have been discarded
- How can I change the name of a data frame
- How to calculate combination and permutation in R?
- In R, dealing with Error: ggplot2 doesn’t know how to deal with data of class numeric