gpt4 book ai didi

r - ggplot2中的堆叠条形图:一次打印标签,而不是两次

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

我用R中的ggplot2创建了一个堆叠的barplot。这两层代表两个时间点的实验得分,这里称为“Main2”和“Main4”。您在图表中(条形图上或条形图旁边)看到的标签是我每个单词获得的观察数。 Main2和Main4上的此数字始终相同。因此,我只想每个单词打印一次,而不是两次。我该怎么做呢?

这是生成绘图的代码:

ggplot(data_words, aes(x = Word, y = Score * 100, fill = Cognate)) + 
geom_bar(aes(group = TestingMoment), stat = "identity", position = "identity", alpha = 0.5) +
geom_text(aes(label = freq), size = 3, position = position_nudge(y=2)) +
theme(axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 10),
text = element_text(size = 15)) +
labs(x = "Word\n", y = "\nAverage score at Main2 and Main4") +
scale_fill_manual(values=c("#000000", "#56B4E9")) +
coord_flip()

这是数据集的一部分,因此您可以看到以下格式:
             Word     Cognate TestingMoment     Score freq
1 zwemvleugel Cognate Main4 0.9900000 10
2 zwemvleugel Cognate Main2 0.9900000 10
3 zeis Cognate Main4 0.5558333 12
4 zeis Cognate Main2 0.4791667 12
5 zaag Cognate Main4 0.7572727 11
6 zaag Cognate Main2 0.5336364 11
7 wip Cognate Main4 1.0000000 12
8 wip Cognate Main2 0.9166667 12
9 wafelijzer Cognate Main2 0.6985714 7
10 wafelijzer Cognate Main4 0.8414286 7
11 waaier Non-cognate Main2 0.6666667 15
12 waaier Non-cognate Main4 0.8666667 15
dput的结果:
structure(list(Word = c("zwemvleugel", "zwemvleugel", "zeis", 
"zeis", "zaag", "zaag", "wip", "wip", "wafelijzer", "wafelijzer",
"waaier", "waaier"), Cognate = c("Cognate", "Cognate", "Cognate",
"Cognate", "Cognate", "Cognate", "Cognate", "Cognate", "Cognate",
"Cognate", "Non-cognate", "Non-cognate"), TestingMoment = c("Main4",
"Main2", "Main4", "Main2", "Main4", "Main2", "Main4", "Main2",
"Main2", "Main4", "Main2", "Main4"), Score = c(0.99, 0.99, 0.5558333,
0.4791667, 0.7572727, 0.5336364, 1, 0.9166667, 0.6985714, 0.8414286,
0.6666667, 0.8666667), freq = c(10, 10, 12, 12, 11, 11, 12, 12,
7, 7, 15, 15)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-12L), .Names = c("Word", "Cognate", "TestingMoment", "Score",
"freq"))

编辑:如果有人可以指出如何增加两层之间的对比度,那也会有所帮助。

Barplot

最佳答案

由于您没有提供可复制的示例,因此请尝试一下,但无法对其进行测试。

ggplot(data_words, aes(x = Word, y = Score * 100, fill = Cognate)) + 
geom_bar(aes(group = TestingMoment), stat = "identity", position = "identity", alpha = 0.5) +
geom_text(data=subset(data_words, TestingMoment == 'Main4'),
aes(label = freq), size = 3, position = position_nudge(y=2)) +
theme(axis.text.x = element_text(size = 13),
axis.text.y = element_text(size = 10),
text = element_text(size = 15)) +
labs(x = "Word\n", y = "\nAverage score at Main2 and Main4") +
scale_fill_manual(values=c("#000000", "#56B4E9")) +
coord_flip()

因此,我不确定 TestingMoment是否是要过滤的正确变量,但我想您已经明白了。

您可以在条形图内使用 color来获得条形图的轮廓并增加对比度。

也许考虑使用 facet_wrap以获得更多的方面。

关于r - ggplot2中的堆叠条形图:一次打印标签,而不是两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49799859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com