This is something of an extension of a previous question here:
Assign colors to a data frame based on shared values with a character string in R
I now have a data frame with x, y, errrs, and newcolors and I want to plot the data and error bars by color using ggplot2. I've tried this, and the plot works well, but the colors are not even close to being correct. I've tried defining the color variables in different places within the ggplot() call, but no luck. What am I missing?
Here is the data:
names <- c( "TC3", "102", "172", "136", "142", "143", "AC2G" )
colors <- c( "darkorange", "forestgreen", "darkolivegreen", "darkgreen", "darksalmon", "firebrick3", "firebrick1" )
dataA <- c( "JR13-101A", "TC3B", "JR12-136C", "AC2GA", "TC3A" )
newcolors <- rep( NA, length( dataA ) )
dataA <- as.data.frame( cbind( dataA, newcolors ) )
x <- c( 1, 2, 3, 4, 5 )
y <- c( 10, 6, 3, 18, 2 )
errs <- c( 2, 1, 2, 1, 2 )
dataA <- cbind( dataA, x, y, errs )
and a solution to my previous question by @Dave2e that assigns colors by sample name:
dataA$newcolors <- as.character( dataA$newcolors )
for( j in 1:length( names ) ) {
dataA$newcolors[ grep( names[ j ], dataA$dataA ) ] <- colors[ j ]
}
and finally the plotting code I've tried:
ggplot( dataA, aes( x = x, y = y) ) +
geom_errorbar( aes( ymin = y - errs, ymax = y + errs, color = newcolors ),
width = 0.03 ) +
geom_point( size = 5, aes( color = newcolors) )
(I've also tried putting the colors into the aes() call up front by aes( x = x, y = y, color = newcolors). The plot looks good, apart from the fact that the colors are not correct. "darkorange" shows up as a light green "darkgreen" is some pinkish color, and "firebrick1" is a light blue.


scale_color_manual?scale_colour_manualor usecolour = I(newColours)in the aes to use the colours as coloursI(newcolors)tool. Thanks for clearing it up for me.scale_color_manualto plot these, and theI(newcolors)call plots solid symbols, as opposed to symbols with a black border. How would I define these?