I have a data frame D
that look 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 plot 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 looks like this:
However I want to assign the color white to variable B. Is this manipulation possible? Additionally I am using dummy data for simplicity's sake in this post. The actual data that I will be using may have 10 or more different variables under Legend, so writing out each hex color doesn't look like a clean option.
Thank you for the help
Does this produce what you're after?
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()
assigning colors to A, B, and C. I also added colour="black"
to geom_bar()
. You could try it with and without the second part and see what you think.
You can use scale_fill_manual(values=c( .. and then your hex colors"
ggplot(D, aes(x=Track, y=Length, fill=Legend)) +
geom_bar(stat='identity') + scale_fill_manual(values=c("#000000","#FFFFFF", "#00FF00"))
Also can 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With