使用R中的ggplot2修改图例

时间:2011-07-16 21:16:20

标签: r ggplot2 histogram

1 个答案:

答案 0 :(得分:4)

也许是这样的......

dat = data.frame(x=rnorm(1000))  
ggplot(dat,aes(x=x)) + 
    geom_histogram(aes(y=..density..,fill="Histogram"),binwidth=0.5) + 
    stat_function(fun = dnorm, aes(colour= "Density")) +
    scale_x_continuous('x', limits = c(-4, 4)) + 
    opts(title = "Histogram with Overlay") +
    scale_fill_manual(name="",value="blue") + 
    scale_colour_manual(name="",value="red") + 
    scale_y_continuous('Frequency')+
    opts(legend.key=theme_rect(fill="white",colour="white"))+
    opts(legend.background = theme_blank())

注意:由于版本0.9.2 optsreplaced theme。例如,上面的最后两行是:

theme(legend.key = element_rect(fill = "white",colour = "white")) + 
theme(legend.background = element_blank())