2017-07-05 24 views
0

私は、次のデータセットを持っている:ggplotly棒グラフと折れ線グラフ

 dput(head(table_age)) 
structure(list(Age = c("0-19", "20-24", "25-29", "30-34", "35-39", 
"40-44"), Freq_nominal = c(1321, 3446, 5354, 7233, 7471, 6623 
), Freq_percent = c(1.92, 5.01, 7.79, 10.52, 10.86, 9.63), Percent = c("1.92%", 
"5.01%", "7.79%", "10.52%", "10.86%", "9.63%"), Nominal = c(1321, 
3446, 5354, 7233, 7471, 6623), female = c(655L, 1473L, 2275L, 
2929L, 3081L, 2830L), male = c(665L, 1918L, 3002L, 4258L, 4347L, 
3762L), female_percent = c(2.2, 4.94, 7.63, 9.82, 10.33, 9.49 
), female_percent_label = c("2.2%", "4.94%", "7.63%", "9.82%", 
"10.33%", "9.49%"), male_percent = c(1.73, 4.99, 7.8, 11.07, 
11.3, 9.78), male_percent_label = c("1.73%", "4.99%", "7.8%", 
"11.07%", "11.3%", "9.78%")), .Names = c("Age", "Freq_nominal", 
"Freq_percent", "Percent", "Nominal", "female", "male", "female_percent", 
"female_percent_label", "male_percent", "male_percent_label"), row.names = c(NA, 
6L), class = "data.frame") 

は私が棒グラフとしてinteractivlyプロットにggplotly年齢を使用し、テキストparametarとツールチップで表示カスタムラベル。

これは、作業コードです:

t <- ggplot(table_age, aes(as.factor(Age), Freq_percent, text1 = Nominal, 
          text = paste0("Age group: ", Age), text2 = Percent)) + 

    geom_bar(position="dodge",stat="identity", fill = "deepskyblue4", width = 0.5) + 

    theme(legend.title=element_blank()) + 
    xlab("") + ylab("") + 
    theme_minimal() 

ggplotly(t, tooltip = c("text1", "text2")) 

私は年齢の棒グラフの上に男女それぞれの行/ポイントをオーバーレイしたいです。その点で、私は2つの問題を抱えている:

  1. geom_pointは私がgeom_bar
  2. から別々geom_pointsにラベルを付ける方法がわからない
  3. plotlyてプロットしたが、(それが正常に動作したときに、静的なプロット)geom_lineないます

これは私の試みコードです:

t <- ggplot(table_age, aes(as.factor(Age), Freq_percent, text1 = Nominal, 
          text = paste0("Age group: ", Age), text2 = Percent)) + 

    geom_bar(position="dodge",stat="identity", fill = "deepskyblue4", width = 0.5) + 

    theme(legend.title=element_blank()) + 
    xlab("") + ylab("") + 
    theme_minimal() 

t <- t + geom_point(data=table_age, aes(x=Age, y=female_percent), size = 2, color="magenta") 
t <- t + geom_line(data=table_age, aes(x=Age, y=female_percent), colour="magenta") 

ggplotly(t, tooltip = c("text1", "text2", "female")) 

任意のアイデア?

+1

あなたが '' dput(table_age)の結果を追加することによって、あなたのコードは、再現してくださいすることができ質問? – GGamba

+0

コメントありがとうございます。私はちょうど質問を更新しました。 – Prometheus

答えて

2

geom_line()(通常は)groupの美学が必要です。これは、1つのobsグループを持つ定数に設定できます。これはgeom_lineと表示されます。

さまざまなツールチップを表示するには、「隠された」text(すでに行ったように)を使用して上書きすることができます。

この場合、最初にggplot()コールを定義するのではなく、geom内にaesをそれぞれ定義する方がよいでしょう。コードの上

library(ggplot2) 
library(plotly) 

t <- ggplot(table_age) + 
    geom_bar(aes(as.factor(Age), 
       Freq_percent, 
       #text = paste0("Age group: ", Age), 
       text1 = Nominal, 
       text2 = Percent), 
      position = "dodge", 
      stat = "identity", 
      fill = "deepskyblue4", 
      width = 0.5) + 
    geom_point(aes(as.factor(Age), 
        female_percent, 
        text1 = female), 
       size = 2, 
       color="magenta") + 
    geom_line(aes(as.factor(Age), 
        female_percent, 
        group = 1), 
       color = "magenta") + 
    theme(legend.title = element_blank()) + 
    xlab("") + 
    ylab("") + 
    theme_minimal() 
#> Warning: Ignoring unknown aesthetics: text1, text2 
#> Warning: Ignoring unknown aesthetics: text1 

t 

ggplotly(t, tooltip = c("text1", "text2")) 

enter image description here

+0

解決に感謝します。また、コードはきちんと構造化されています。私はそれにもいくつかの努力をする必要があります。 – Prometheus

関連する問題