2016-06-29 4 views
5

私は2年以上にわたって質問された得点を持つデータセットを持っています。各質問には2015の値と2016の値があります。私はそれぞれをプロットし、2015値と2016値の間に違う値を表示したいと思います。スコアは上がったり下がったり、同じままになったのですか?私は、変化の方向を示すためにポイントのペアを線(または矢印)で結ぶと便利かもしれないと思っていましたが、これを行うにはggplotを得るのが苦労しています。ここに私のコード例があります:ggplot2:時間とともに値の差を矢印で表示する

df <- read.table(text = "question y2015 y2016 
q1 90 50 
q2 80 60 
q3 70 90 
q4 90 60 
q5 30 20", header = TRUE) 

g1 <- ggplot(df, aes(x=question)) 
g1 <- g1 + geom_point(aes(y=y2015, color="y2015"), size=4) 
g1 <- g1 + geom_point(aes(y=y2016, color="y2016"), size=4) 
g1 

これを視覚化するためのさまざまなアプローチは大歓迎です。

+0

私は(2015のスコアの散布を行うだろう、私は今の答えを書く時間がありませんが、質問のかなりの数を持っている場合(あなたが以下のコメントで100〜言及) x)対2016スコア(y)。 45度線で追加すると線の上の点が改善され、2年間の相関がはっきりとわかります(外れ値も目立つべきです)。 – Gregor

+0

@自己;あなたにとって面白いかもしれないhttp://stackoverflow.com/questions/38109623/remove-legend-elements-of-one-specific-geom-show-legend-false-does-not-do-t/38110017#38110017 – Alex

答えて

2

それはまだ少し醜いですし、微調整を必要とするが、それは矢を持って、あなたは質問によってファセットと上の年に置く場合)

library(ggplot2) 
library(reshape2) 
library(dplyr) 

ggplot2df <- read.table(text = "question y2015 y2016 
q1 90 50 
       q2 80 60 
       q3 70 90 
       q4 90 60 
       q5 30 20", header = TRUE) 


df <- ggplot2df %>% 
    mutate(direction = ifelse(y2016 - y2015 > 0, "Up", "Down"))%>% 
    melt(id = c("question", "direction")) 


g1 <- ggplot(df, aes(x=question, y = value, color = variable, group = question)) + 
    geom_point(size=4) + 
    geom_path(aes(color = direction), arrow=arrow()) 

enter image description here

+0

非常にクールです。私は本当にこれが好きです。 – oneself

1

多分このような何か? 非常に有用なライブラリtidyrから、データの一部を再形成する必要があり、関数gatherが必要です。

library(tidyr) 
library(ggplot2) 

g1 <- df %>% gather(year, value, y2015:y2016) %>% 
ggplot(aes(x = year, y = value, color= question)) + 
    geom_point() + 
    geom_line(aes(group=interaction(question))) 
g1 

enter image description here

+1

これはとても素敵です。増加していた1つの質問を特定するのは簡単です。 – bouncyball

+2

これはいいですね。しかし、実際のデータでは、より多くの質問があり(〜100)、それぞれのテキストは「q1」よりも長くなります。だから、私はこのように視覚化することはあまりにも混乱していると思う。 – oneself

3

私は "ダンベル" チャートはあまりにも、働くだろうと思います。ここで私は長い間あなたのデータを再構成しました。

df <- read.table(text = "question y2015 y2016 
q1 90 50 
q2 80 60 
q3 70 90 
q4 90 60 
q5 30 20", header = TRUE) 

df.long <- 
    reshape(df, varying = names(df)[2:3], 
     direction = 'long', 
     #ids = 'question', 
     times = 2015:2016, 
     v.names = 'perc', 
     timevar = 'year' 
     ) 

ggplot(df.long, aes(x = perc, y = question))+ 
    geom_line(aes(group = question))+ 
    geom_point(aes(colour = factor(year)), size = 2)+ 
    theme_bw()+ 
    scale_color_brewer(palette = 'Set1', name = 'Year') 

enter image description here

3

x軸では、トレンドの方向を色で強調表示し、x軸で時間の経過を表示することができます。美学のこのエンコーディングで

library(reshape2) 
library(dplyr) 
library(ggthemes) 

ggplot(df %>% melt(id.var="question") %>% 
     group_by(question) %>% 
     mutate(Direction=ifelse(diff(value)>0,"Up","Down")), 
     aes(x=gsub("y","",variable), y=value, color=Direction, group=question)) + 
    geom_point(size=2) + 
    geom_path(arrow=arrow(length=unit(0.1,"in")), show.legend=FALSE) + 
    facet_grid(. ~ question) + 
    theme_tufte() + 
    theme(strip.text.x=element_text(size=15)) + 
    guides(color=guide_legend(reverse=TRUE)) + 
    scale_y_continuous(limits=c(0,100)) + 
    labs(x="Year", y="Value") 

、おそらく伝説を必要としない、と線分に矢印を追加するだけでなく、余分かもしれないが、私は説明のためにそれらを残してきました。

enter image description here