我使用下面的数据:调整传说和颜色GGPLOT2
testdf = structure(list(var1 = c(14.9, 15.5, 16.5, 16.6, 15.1, 13.8, 13.2,
27.6, 22.3, 29.1, 18.4, 14.8, 15.7, 14.3, 15.5, 15.8, 17.6, 14.9,
16.9, 20.8, 13.9, 20.1, 16.9, 24.7, 15.2, 15.9, 15.8, 17.1, 15.9,
17.3, 17.5, 14.7, 21, 12, 18.6, 16.1, 16.1, 15.8, 15.9, 13.9,
13.6, 13.6, 14.2, 13.9, 14.1, 13.9, 13.7, 13.6, 13.9, 13.2),
age = c(7L, 7L, 8L, 10L, 7L, 11L, 9L, 14L, 12L, 15L, 10L,
12L, 12L, 9L, 9L, 10L, 15L, 10L, 12L, 14L, 15L, 13L, 15L,
13L, 11L, 9L, 14L, 12L, 12L, 15L, 13L, 12L, 15L, 7L, 14L,
8L, 10L, 8L, 9L, 9L, 8L, 10L, 9L, 9L, 11L, 10L, 10L, 9L,
9L, 9L)), .Names = c("var1", "age"), row.names = c(NA, 50L
), class = "data.frame")
我可以有下面的代码直方图:
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age))
但我怎么能得到青睐7,8,9, 10,11,12,13,14,15在传说和不同颜色的所有这些年龄组,如彩虹(9)
我尝试下面的代码,但他们只是部分工作:
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age))+scale_colour_continuous(breaks=c(7:15),color=rainbow(9))
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age, legend=F))+scale_colour_continuous(breaks=c(7:15))
我认为问题是,既然'age'是数值变量它不被视为一个独立因素。试着做'testdf $ age < - factor(testdf $ age)',然后你的原始绘图命令应该可以工作。 – nrussell
是的,它的工作原理。谢谢。 – rnso