r - Filling histogram by column of color names -
i have function combines data sets , assigns colors each code seen below
x1 <- rep(1:10, each = 3, times = 3) x2 <- rep(1:7, each = 7, times = 2) x3 <- rep(6:9, each = 11, times = 4) y1 <- rep(1:5, each = 2, times = 9) y2 <- rep(1:14, times = 7) y3 <- rep(1:11, times = 16) color1 <- rep("blue", times = length(x1)) color2 <- rep("red", times = length(x2)) color3 <- rep ("green", times = length(x3)) data1 <- cbind(x1, y1, color1) data2 <- cbind(x2, y2, color2) data3 <- cbind(x3, y3, color3) alldata <- data.frame(rbind(data1, data2, data3)) colnames(alldata) <- c("x", "y", "color") ggplot(data = alldata, aes(x=x), position = "dodge") + geom_histogram(fill = alldata$color) ggplot(data = alldata, aes(x=x, y=y)) + geom_point(colour = alldata$color)
i wondering why colors aren't being assigned histogram, being supplied points of scatter plot.
the error i'm getting is
error: incompatible lengths set aesthetics: fill
essentially data being grouped color, , want color assigned each group color represented in histogram.
you want map color variable fill , use values without scaling:
ggplot(alldata, aes(x=x, fill = color)) + geom_histogram(position = "dodge") + scale_fill_identity()
Comments
Post a Comment