我在R中创建了一个带有ggplot2的叠加条形图。这两个图层代表了两个时间点的实验得分,这里称为“Main2”和“#2”。和' Main4'。您在图表中看到的标签(在条形图上或旁边)是我每个单词获得的观察数量。 Main2和Main4上的此数字始终相同。因此,我想每个字只打印一次,而不是两次。我该怎么做?
这是生成图表的代码:
string name;
long long maps;
int i,j,test,l,counts=0,k;
cin>>test;
for(i=0;i<test;i++){
counts=maps=0;
cin>>name;
l = name.length();
for(j=0;j<l;j++){
k=(toupper(name[j])-'A');
cout<<endl<<(maps&(1<<k))<<" "<<k;
if(0 == maps&(1<<k)){
counts++;
maps|=(1<<k);
}else{
cout<<" "<<(int)(maps&(1<<k))==0;
}
}
cout<<counts;
}
这是数据集的一部分,因此您可以获得格式的印象:
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
编辑:如果有人可以指出如何增加两层之间的对比度,那也会有所帮助。
答案 0 :(得分:2)
由于您没有提供可重现的示例,我试一试,但无法测试它。
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
以获得额外的方面。
答案 1 :(得分:2)
在致电geom_bar
时,您基本上会复制geom_col
的内容 - 这是geom_bar
的特例。它更快,并获得相同的结果,只使用geom_col
。
我认为你也可能想要position = "dodge"
,而不是position = "identity"
所以你的酒吧彼此相邻而不是彼此重叠......也许我错了。
在geom_text
中,我指定数据应该是通过ggplot
对象传递的相同数据,但仅针对Main4观察值进行过滤,作为示例。您可以通过其他方式进行操作---过滤Main2,按Word分组并使用top_n
获取分数最高的那个,等等。您可以对文本定位进行一些调整,但这里&# 39;粗剪。
library(tidyverse)
df <- 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"))
ggplot(df, aes(x = Word, y = Score * 100, fill = Cognate, group = TestingMoment)) +
geom_col(position = "dodge") +
geom_text(aes(label = freq), size = 3, nudge_y = 4, data = . %>% filter(TestingMoment == "Main4"), vjust = 1) +
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()
由reprex package(v0.2.0)创建于2018-04-12。
答案 2 :(得分:1)
仅使用geom_text
图层所需的行。
例如,使用filter
中的dplyr
:
... +
geom_text(aes(label = freq),
size = 3,
position = position_nudge(y=2),
data = filter(data_words, TestingMoment == "Main2")) +
答案 3 :(得分:1)
我猜你只想要最高分的标签。所以只需过滤那些:
df %>%
group_by(Word, Cognate) %>%
arrange(desc(Score)) %>%
filter(row_number() == 1)