2012-09-07 18 views
6

凡例のキーを構成する灰色の背景に色付きの線を使用する代わりに、キーのラベルの隣に色の四角または円を配置して、容易に目に見える。どうやってやるの?ここでは一例として使用するコードスニペットです:ggplotの凡例の色を四角または円に変更する

mry <- do.call(rbind, by(movies, round(movies$rating), function(df) { 
    nums <- tapply(df$length, df$year, length) 
    data.frame(rating=round(df$rating[1]), year = as.numeric(names(nums)), 
    number=as.vector(nums)) 
})) 

ggplot(mry, aes(x=year, y=number, colour=factor(rating))) + geom_line() + 
scale_colour_brewer(palette="YlGnBu") 

答えて

4

1つのハックは、円を作るために上がってくる....

ggplot(mry, aes(x=year, y=number, colour=factor(rating))) + 
    scale_colour_brewer(palette="YlGnBu") + 
    geom_point() +  
    geom_point(size=5,colour="white",show_guide=FALSE) + 
    opts(
    panel.background = theme_rect(fill = "transparent"), 
    panel.grid.minor = theme_blank(), 
    panel.grid.major = theme_blank(), 
    plot.background = theme_rect(fill = "transparent",colour = NA) 
) + geom_line(show_guide=FALSE) 

enter image description here

+0

あなたがの形状を変化させることによって、正方形などを作ることができます点 –

+0

円と四角の大きさも簡単に編集できます。 –

+0

**更新** 'opts'は今度は削除され、' theme'が使用され、 'theme_rect'は' element_rect'などに置き換えられます。 –

関連する問題