gpt4 book ai didi

r - ggplot图中带有两个标签的Geom_text

转载 作者:行者123 更新时间:2023-12-02 08:12:20 27 4
gpt4 key购买 nike

我正在努力添加一个额外的标签,我想添加到我的 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))

我在 paste() 参数中使用了 geom_text(): label 函数,其中实现了两个变量值(计数和持续时间)。

enter image description here

关于r - ggplot图中带有两个标签的Geom_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45437815/

27 4 0