1

散布図にカラーバーを追加したいと考えています。現在、カラーバーに変更する必要がある個別の凡例が生成されています。これで私を助けてください。前もって感謝します、

library(ggplot2)    
x_axis<-sample(1:55)
y_axis<-sample(100:154)

plotName<-'Scatter plot with colorbar instead of discrete legend'
color_plate <- colorRampPalette(colors = c('dodgerblue4','dodgerblue','green3','green2','yellow2','red','darkred'),space='Lab') #v1
color_plate <- color_plate(55) 


  dat_1 <- data.frame(xvar = x_axis, 
             yvar = y_axis,

col1 <- c(rep(1,8),rep(2,8),rep(3,8),rep(4,8),rep(5,8),rep(6,8),rep(7,7)))              
# col1<-(rep(1,6),rep(2,6),rep(3,6),rep(4,6),rep(5,6),rep(6,6),rep(7,6),rep(8,6),rep(9,7))) 
chart<-ggplot(dat_1, aes(x=xvar, y=yvar)) +
         geom_point(shape=19,size=5,aes(colour = col1)) +

 scale_colour_continuous( low = "blue", high = "red", space ="Lab",name="observation",label=rep("",7) , #nrow(dat_1)
 guide = guide_legend(direction = "horizontal", title.position = "bottom",     title.hjust=0.5) ) +

         theme(legend.position = "bottom") +
         labs(title=plotName)+
         scale_y_continuous(expression(atop('Net'~CO[2]~'Flux '~CO[2]~' (β)' )))+   
         scale_x_continuous(expression(atop('Gross'~CO[2]))) 

print(chart)    
4

1 に答える 1

3

特定の色を選択した理由がわかれば、これは簡単になります。それらが本質的に恣意的である場合、おそらくこれはうまくいくでしょう:

### add a column for color
dat_1 <- data.frame(xvar = x_axis, 
                  yvar = y_axis,
                  col1 = 1:7)

編集これはあなたが望むものに近いはずです

ggplot(dat_1, aes(x=xvar, y=yvar)) +
 geom_point(shape=19, size=5, aes(colour = col1)) +
 scale_colour_continuous( low = "blue", high = "red", space = "Lab",
  name="observation", label=rep("", nrow(dat_1)) ,
  guide = guide_legend(direction = "horizontal", 
   title.position = "bottom",     title.hjust=0.5) ) +
 theme(legend.position = "bottom")  +
 scale_y_continuous(expression(atop('Net'~CO[2]~'Flux '~CO[2]~' (β)' ))) +   
 scale_x_continuous(expression(atop('Gross'~CO[2]))) 

どちらが与える

ここに画像の説明を入力

あなたのコメントに従って""、値として渡すことでラベルを「空白」にしました。外観の変更は、ガイドの凡例のヘルプに従います。55 個の観測値があり、それらすべてに 7 つのカラー値のいずれかを持たせたい場合は、次のようにします。

dat_1$col1 <- rep(seq(7), 8)[-7*8]

あなたのためにそれをするべきです。好みに合わせて色を変更できます。scale_color_brewer 色盲の視聴者に適したものを簡単に確認できるので、個人的には気に入っています。

update2

コードにタイプミスがある可能性があると思います。以下は私にとってはうまくいきます:

set.seed(1)
dat_1 <- data.frame(xvar = sample(1:55),
                    yvar = sample(100:154),
                    col1 = c(seq(10), rep(seq(15), each=3))
                    )

次に、上記のようにプロットすると、15 色相のカラー スケールで 55 ポイントが得られます。

于 2013-09-23T18:56:40.510 に答える