[英]Geom_text with two labels in ggplot graph
我正在嘗試將其他標簽添加到ggplot圖表中。
這是我的數據集:
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))
所需的圖形應包含所有數據點的計數數量(已經存在)以及括號中的持續時間(圖形中標記為紅色)。
如果有人知道如何解決我的代碼,我將不勝感激。
請發布您的數據作為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))
我在geom_text(): label
使用了paste()
函數geom_text(): label
參數,在其中實現了兩個變量值(Counts&Duration)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.