R 为GGPLOT中的特定条指定不同的颜色

R 为GGPLOT中的特定条指定不同的颜色,r,ggplot2,R,Ggplot2,我需要为ggplot bar PLOT中的条形图指定特定颜色。默认情况下,我得到下面的ggplot条形图。在这里,我想要的是为高值条指定不同的颜色(比如红色)。这是针对64项下的条形图,其中类型D用于名称X和Z。对于名称Y,的类型C,在16项下的条形图上也显示红色。目前我正在使用下图所示的R ggplot2代码。请帮助我获取此代码 数据: data <- structure(list(type = structure(c(1L, 2L, 3L, 4L, 2L, 3L, 4L, 3

我需要为ggplot bar PLOT中的条形图指定特定颜色。默认情况下,我得到下面的ggplot条形图。在这里,我想要的是为高值条指定不同的颜色(比如红色)。这是针对
64项
下的条形图,其中
类型D
用于
名称X和Z
。对于
名称Y
类型C
,在
16项下的条形图上也显示红色。目前我正在使用下图所示的R ggplot2代码。请帮助我获取此代码

数据:

data <- structure(list(type = structure(c(1L, 2L, 3L, 4L, 2L, 3L, 4L, 
    3L, 4L, 4L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 3L, 4L, 4L, 1L, 2L, 3L, 
    4L, 2L, 3L, 4L, 3L, 4L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"), 
    items = c(16L, 16L, 16L, 16L, 32L, 32L, 32L, 48L, 48L, 64L, 
    16L, 16L, 16L, 16L, 32L, 32L, 32L, 48L, 48L, 64L, 16L, 16L, 
    16L, 16L, 32L, 32L, 32L, 48L, 48L, 64L), name = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("X", 
    "Y", "Z"), class = "factor"), value = c(6.3, 7.2, 7.1, 7, 
    10, 10.9, 11, 11.6, 11.5, 12, 8.9, 10.6, 11.5, 7.9, 12.6, 
    13.5, 12.4, 13, 14, 15.4, 4.8, 7, 7.2, 6.1, 4.1, 4.4, 4.2, 
    3.2, 2.7, 1.6)), .Names = c("type", "items", "name", "value"
    ), class = "data.frame", row.names = c(NA, -30L))
更新


虽然mnei提供的代码几乎满足了我的需要,但我没有得到图中所示的图例。请告诉我如何获得该图例。

计算每个组合的最大值是哪个
类型

 library(plyr)
 data <- ddply(data, .( items, name), mutate, is_max = type == type[which.max(value)])

## the plot
ggplot(data, aes(x = type, y = value, fill = is_max, group = items)) + 
  facet_grid(name ~ items, scale = 'free', space = 'free') + 
  geom_bar(stat = 'identity') +  
  scale_fill_manual(values = c('black', 'red'), labels = c('other', 'maximum')) +
  opts(legend.position = "top", legend.text = theme_text(size = 15),  
       legend.title = theme_text(size = 0,colour = "white"), 
       legend.key = theme_rect(colour = NA))

谢谢。但它给出了一个错误“错误:手动比例中的值不足。需要3,但只提供2”。请看一看。您使用的是什么版本的
ggplot2
?适用于
ggplot2_0.9.1
>sessionInfo()R版本2.15.1(2012-06-22)平台:i686 pc linux gnu(32位)。。。。。附加基本包:。。。。其他随附包装:[1]plyr_1.7.1 ggplot2_0.9.1hmmm。。。。我添加了一个解决方案,可以避免调用
scale\u fill\u manual
,但我不知道为什么它会失败。修复了--
ddply
调用中的一个小错误。
 library(plyr)
 data <- ddply(data, .( items, name), mutate, is_max = type == type[which.max(value)])

## the plot
ggplot(data, aes(x = type, y = value, fill = is_max, group = items)) + 
  facet_grid(name ~ items, scale = 'free', space = 'free') + 
  geom_bar(stat = 'identity') +  
  scale_fill_manual(values = c('black', 'red'), labels = c('other', 'maximum')) +
  opts(legend.position = "top", legend.text = theme_text(size = 15),  
       legend.title = theme_text(size = 0,colour = "white"), 
       legend.key = theme_rect(colour = NA))
 ggplot(data, aes(x=type, y= value, group = items, fill = type)) +  
  facet_grid(name~items, scale = 'free', space = 'free') + 
  geom_bar(stat= 'identity')  +
  geom_bar(data = data[data$is_max,],stat = 'identity', fill = 'red' ) +
  scale_fill_manual(values = rep("grey60",4), 
                    labels = c("type-A", "type-B", "type-C", "type-D")) + 
  opts(strip.text.y = theme_text(size=14, face="bold")) + 
  opts(legend.position="top", legend.text=theme_text(size=15), 
       legend.title=theme_text(size=0,colour="white"), 
       legend.key = theme_rect(colour = NA))