在ggplot2
中使用R
,我想为每个方面设置fill参数。这是一些简化的代码(Also available at on my GitHub)
# 1. Load libraries
require(ggplot2)
# 2. Make up fake data
yr <- c(2013,2014,2015,2013,2014,2015)
widgets.made <- c(10,20,40,15,30,60)
factory <- c(rep(" Factory A",3),rep("Factory B",3))
df <- data.frame(factory, widgets.made, yr)
# 3. Create a plot with two facets
png("./factories.png")
# Note the geom_bar "fill" parameter-- I want this
# to be different for each facet;
# Red for Factory A
# Blue for Factory B
ggplot(df) +
aes(yr, widgets.made) +
geom_bar(stat="identity", fill="green") +
facet_grid(. ~ factory)
dev.off()
生成的图像看起来像这样(但我想要两种颜色,而不仅仅是一种颜色!):
顺便说一句,这不是功课;我在从事数据科学任务时注意到了这一点,但我并不需要它来完成任务。我只是觉得知道如何做到这一点会很棒。