如何为我的ggplot指定颜色?

时间:2015-09-24 20:38:05

标签: r colors ggplot2

我是使用ggplot的新手。我想要为组指定我想要的颜色(即High = red4; Low = gray45)。组由" high"定义。或"低"值。

ggplot(my_data, aes(x=continuous_variable, fill=Group)) + geom_histogram() + 
  xlab("continuous_variable")+ 
  ylab("Frequency") + 
  ggtitle("My Variable")

1 个答案:

答案 0 :(得分:2)

Axeman指出你已经朝着正确的方向前进了:只需在代码中添加scale_fill_manual

可重复的例子:

library(ggplot2)

# sample data
set.seed(1234)
continuous_variable <- rnorm(100)
Group <- factor(rep(c("high", "low"), 50))
my_data <- data.frame(continuous_variable, Group)

# just add another line to your initial code
ggplot(my_data, aes(x = continuous_variable, fill = Group)) + geom_histogram() + 
  xlab("continuous_variable") + 
  ylab("Frequency") + 
  ggtitle("My Variable") + 
  scale_fill_manual(values = c("high" = "red4", "low" = "grey45"))
#> stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.