2016-12-23 12 views
0

私はプロット持っている:合計が10000と5000色を変更

ありながら

p <- plot_ly(
    x = c(data$sum), 
    y = c(data$candidate), 
    name = "Trump v Clinton", 
    type = "bar" 
) 

CandidateはrealDonaldTrumpとHilaryClintonで各候補のバーの色を設定することが可能ですか?私。トランプは赤、ヒラリーは青。このような何かを探してい

+0

引数 '色=〜candidate'があなたの目的を果たすの追加します。あなたはあなたのデータ( 'data.frame')を持っていなければならず、コード行は' p < - plot_ly(data、x =〜sum、y =〜候補、type = "bar"、name = "Trump v Clinton "、color =〜候補)' – raymkchow

答えて

2

library(plotly) 

data<-data.frame(sum=c(10000, 5000), candidate=c("DonaldTrump", "HilaryClinton")) 
data$color<-c("red", "blue") 

p <- plot_ly(
    y = data$sum, 
    x = data$candidate, 
    color = I(data$color), 
    type = "bar" 
) 
p<-layout(p, title = "Trump vs Clinton") 
print(p) 

を?:この垂直barplotとしてプロット、私は、x軸とy軸の値を交換する必要がありました。

+0

あなたのバーの色は緑と青で、赤と青は予想通りです。 –

0

また、これは動作するはずです:

library(plotly) 
data.frame(sum=c(10000, 5000), candidate=c("DonaldTrump", "HilaryClinton")) %>% 
    plot_ly(x = candidate, y = sum, type = "bar", color = candidate) 

enter image description here