R ggplot Color change of one variable in the stacked histogram

I have a D data frame that looks like this:

 Track <- c(0,0,0,-1,1,1) Length <- c(1,1,2,1,3,1) Legend <- c("A","B","A","C","B","C") D <- data.frame(Track,Length,Legend) D # Track Length Legend # 0 1 A # 0 1 B # 0 2 A # -1 1 C # 1 3 B # 1 1 C 

I am currently using this code to build it:

 ggplot(Z, aes(x=Track, y=Length, fill=Legend)) + geom_bar(stat='identity') + geom_point() + expand_limits(x=0,y=0) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black")) 

And the graph is as follows: enter image description here

However, I want to assign the color white to variable B. Is this manipulation possible? In addition, I use dummy data for simplicity in this article. The actual data that I will use may contain 10 or more variables in the Legend section, so recording every hex color does not look like a clean option.

thanks for the help

+5
source share
2 answers

Does it get what you need?

 x <- length(levels(factor(Legend))) x.colors <- hcl(h=seq(15,375,length=(x+1)),l=65,c=100)[1:x] x.colors[x] <- "white" ggplot(D, aes(x=Track, y=Length, fill=Legend)) + geom_bar(stat='identity',colour="black") + geom_point() + expand_limits(x=0,y=0) + scale_fill_manual(values=x.colors) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour ="black")) 

I added scale_fill_manual() assign colors A, B, and C. I also added colour="black" to geom_bar() . You can try it with and without the second part and see what you think.

+2
source

You can use scale_fill_manual (values ​​= c (.. and then your hexadecimal colors "

  ggplot(D, aes(x=Track, y=Length, fill=Legend)) + geom_bar(stat='identity') + scale_fill_manual(values=c("#000000","#FFFFFF", "#00FF00")) 

You can also use

 # The palette with grey: cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") # The palette with black: cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") # To use for fills, add scale_fill_manual(values=cbPalette) # Use a different gradient scale_colour_gradientn(colours=rainbow(4)) # To use for line and point colors, add scale_colour_manual(values=cbPalette) 
+2
source

All Articles