2017-08-01 55 views
0

我正在努力添加一個我想添加到我的ggplot圖中的標籤。在ggplot圖中有兩個標籤的Geom_text

這裏是我的數據集:

Group     Gaze direction Counts Duration 
Expert Performers  game table  148  1262.122 
Expert Performers  objects table 40  139.466 
Expert Performers  other   94  371.191 
Expert Performers  co-participant 166  387.228 
Non-Performers   game table  223  1137.517 
Non-Performers   objects table 111  369.26 
Non-Performers   other   86  86.794 
Non-Performers   co-participant 312  566.438 

這是我使用的代碼:

ggplot(b, aes(x=Group, y=Gaze.direction))+ 
    geom_count(mapping=aes(color=Counts, size=Duration))+ 
    theme_bw()+ 
    theme(panel.grid.major = element_line(colour = "grey"))+scale_size(range = c(0, 8))+ 
    scale_colour_gradient(low = "black", high = "gray91")+ 
    scale_y_discrete(name ="Gaze direction") + 
    geom_text(aes(label=Counts,hjust=-1, vjust=-1)) 

Current plot:

理想的圖形應包含計數的數量(這是已經存在)以及括號中的持續時間(圖中標記爲紅色)用於所有數據點。

Desired plot:

如果有一個人,誰擁有一個想法,我怎麼能修復我的代碼,我將非常感激。

回答

0

請將您的數據作爲dput()輸出!

你可以試試這個:

library(ggplot2) 
b <- data.frame(Group = c("Expert Performers","Expert Performers","Expert Performers","Expert Performers","Non-Performers","Non-Performers","Non-Performers","Non-Performers"), 
        Gaze.direction = c("game table","objects table","other","co-participant","game table","objects table","other","co-participant"), Counts = c(148,40,94,166,223,111,86,312), Duration =c(1262.122,139.466,371.191,387.228,1137.517,369.26,86.794,566.438)) 

ggplot(b, aes(x=Group, y=Gaze.direction))+ 
    geom_count(mapping=aes(color=Counts, size=Duration))+ 
    theme_bw()+ 
    theme(panel.grid.major = element_line(colour = "grey"))+scale_size(range = c(0, 8))+ 
    scale_colour_gradient(low = "black", high = "gray91")+ 
    scale_y_discrete(name ="Gaze direction") + 
    geom_text(aes(label=paste("(",Counts,",",Duration,")"),hjust=-1, vjust=-1)) 

我用paste()功能geom_text(): label參數,其中兩個您的變量值(計數&時間)的實現。

enter image description here

+0

對不起'dput'輸出。不是故意的。下一次,我會更加關注它。非常感謝解決方案。它看起來正是我想要擁有它的方式。 – user3832272

+0

唯一的問題是,我仍然錯過了持續時間後的'ms'。此外,括號內和計數之間以及其他地方都有空間。有沒有辦法刪除這個空的空間? – user3832272

+0

使用geom_text(aes(label = paste0(「(」,Counts,「,」,Duration,「ms)」)... –