下面是我用作示例的可重现数据。组类似因素 - 填充ggplot2
Name <- c("Blueberry", "Raspberry", "Celery", "Apples", "Peppers")
Class <- c("Berries", "Berries", "Vegetable", "Fruit", "Vegetable")
Yield <- c(30, 20, 15, 25, 40)
example <- data.frame(Class = Class, Name = Name, Yield = Yield)
当ggplot2
绘制我们得到...
ggplot(example, aes(x = Name, y = Yield, fill = Name))+
geom_bar(stat = "identity")
这将是有益的,如果我们可以给那些具有相同的类相似颜色的填充。例如,如果蔬菜为蓝色,浆果为粉红色,果实为绿色,您可以看到植物种类的产量,但仍然在视觉上看到名称(这对我们来说更重要)
I觉得我可以scale_fill_hue()
做到这一点,但我似乎无法得到它的工作
ggplot(example, aes(x = Name, y = Yield))+
geom_bar(aes(fill = Class),stat = "identity")+
scale_fill_hue("Name")