2016-05-16 4 views
2

私は、Rshinyとプロットして、テキストラベル付きの散布図を作成しています。以下は、再現性の例である:Rshinyでプロットされた点とテキスト

library(ggplot2) 
library(plotly) 
dat <- data.frame(LongExpressionValue = rnorm(1:100), 
        LongMethylationValue = rnorm(1:100), 
        LongCopyNumberValue = rnorm(1:100)) 

rownames(dat) <- paste0('n',seq(1:100)) 

# ggplot 
p <- ggplot(data = dat, aes(x = LongExpressionValue, y = LongMethylationValue)) + 
    geom_point(size = 2) + geom_smooth(method = lm) + 
    geom_text(aes(label = rownames(dat)), vjust=-1.5, size = 3) 

# ggplotly 
ggplotly(p) 

これは、のようなプロット作成します。

enter image description here

をラベルが上記表示され、ポイントを重複しないように、どのように私は私のgeom_textオプションを調整できますか?私はggplotコードをアプリケーション間で使用するために保持したいと思います。

ありがとうございます!

+0

は、それは 'ggplot2'を使用し、直接' plot_ly'を呼び出さないことが必要ですか?私はそれらのためのソリューションがはるかに簡単であることを見つけるconfigsすべてリストのリストだけです。特に、plotlyのサイトのリファレンスページは、すべての部品の配置を可能にし、hoverinfoとテキストオプションは驚くべきです – Shape

+0

あなたは'aes'ではなく' aes_string'ですか? –

+0

プログラムで生成されたプロットで作業する必要がある場合は、通常は 'as.name'を使用します。 – Shape

答えて

2

はこれを試してください:あなたがソースに直接行くことができるとき

plot_ly(data = dat, 
     x = LongExpressionValue, 
     y = LongMethylationValue, 
     text = rownames(dat), 
     marker = list(size = 10), 
     mode = "markers+text", 
     textposition = 'top center') 

それはggplot2で働きすぎ価値はありません。これは非常に貴重です:plot_lyまたはlayouthttps://plot.ly/r/reference/

すべてはリストのリストであるので、あなたは簡単にパラメータを設定することができます(予告marker = list(size = 10)

EDIT:hoverinfo +テキストのパワーを示すやや複雑1一緒:

plot_ly(data = dat, 
     x = LongExpressionValue, 
     y = LongMethylationValue, 
     text = paste0(rownames(dat), 
         '<br>A:', 1:nrow(dat), #Examples of additional text 
         '<br>B:', sample(nrow(dat))), #Examples of additional text 
     hoverinfo = 'text+x+y', 
     marker = list(size = 10), 
     mode = "markers+text", 
     hoverinfo = 'text', 
     textposition = 'top right') 
+1

まあまあ私はそれを試してみましょう。私はちょうどggplot2のすべてのコードを持っていたので、ggplotly()関数を追加して私の既存のプロットをプロットに変換した方が簡単だと思いました。 –

+0

上にテキストを表示する必要がなく、ホバーオーバーの一部であることが望ましい場合は、モードから「+テキスト」を削除することをおすすめします。 – Shape

1

あなたはggplot2に固執し、あなたの滑らかなラインを維持したい場合は、あなたが少しあなたのポイントのサイズを大きくし、各ポイントの内側にあなたのラベルを追加することができます。

p <- ggplot(data = dat, aes(x = LongExpressionValue, y = LongMethylationValue)) + 
    geom_point(size = 7) + geom_smooth(method = lm) + 
    geom_text(aes(label = rownames(dat)), size = 2, color="white") 

# ggplotly 
ggplotly(p) 

enter image description here

+0

残念ながら、変数名。たとえば、n1の代わりに、私はこの異名を持つことができます。そしてそれは円に収まりません。 –

1

あなたがplotly_buildリストにtextposition要素を追加する必要がggplot2に滞在したい場合。 hjustおよびvjustオプションが設定されていても、元のplotly_buildにはtextpositionがありません。

だから、これは動作します:

#devtools::install_github("ropensci/plotly") # if not already done 
library(ggplot2) 
library(plotly) 
dat <- data.frame(LongExpressionValue = rnorm(1:100), 
       LongMethylationValue = rnorm(1:100), 
       LongCopyNumberValue = rnorm(1:100)) 

rownames(dat) <- paste0('n',seq(1:100)) 

gg <- ggplot(data = dat, aes(x = LongExpressionValue, y =LongMethylationValue)) + 
geom_point(size = 1) + 
geom_smooth(method = lm) + 
geom_text(aes(label = rownames(dat)), size = 3) 

p <- plotly_build(gg) 

length<-length(p$x$data) 
invisible(lapply(1:length, function(x) p$x$data[[x]]<<-c(p$x$data[[x]], textposition ='top center'))) 

p 

関連する問題