3

如果这是一个重复的问题,我真的很抱歉。但由于某种原因,之前给出的答案对我不起作用。我有以下df:

Country<-c("Chile", "Chile", "Finland","Finland")
Taxa<-c("Mammalia","Amphibia","Mammalia","Amphibia")
Loss<-c(0.15, 1, 0.2, 0.75)
df<-data.frame(Country, Taxa, Loss)

我想将其显示为排名并添加标签。这就是我得到的:

ggplot(df,aes(x=reorder(Country, Loss), y=Loss)) + 
  geom_bar(aes(fill = Taxa), position="dodge", stat="identity") +
  labs(x = "", y = "")+
  ylim(0,1)+
  theme (legend.position="top", legend.title = element_blank(),legend.text = element_text(size = 17),
         axis.text.x  = element_text(size=17), 
         axis.text.y  = element_text(size=17), axis.title.y=element_text(size=17))+
  geom_text(aes(label = Loss), position=position_dodge(width=1))+
  coord_flip()

它工作正常!只是我不能像我想要的那样定位标签。我更喜欢标签旁边的栏。width我试着和一些一起玩vjusthjust但位置从未改变......我做错了什么?

先感谢您!!!

4

2 回答 2

4

正如您提到的,另一个使用vjustand的解决方案。hjust

如何使用vjusthjust

我想你想要每个栏都有一个标签,group = Taxageom_text(aes()).

在此处输入图像描述

ggplot(df,aes(x=reorder(Country, Loss), y=Loss)) +   
geom_bar(aes(fill = Taxa), position="dodge", stat="identity") + 
labs(x = "", y = "")+  ylim(0,1)+  theme (legend.position="top", 
legend.title = element_blank(),legend.text = element_text(size = 17),
axis.text.x  = element_text(size=17), axis.text.y  = element_text(size=17), 
axis.title.y=element_text(size=17))+ 
geom_text(aes(x = reorder(Country, Loss), label = Loss, group = Taxa), 
position=position_dodge(width=1), hjust = -1) 
+  coord_flip()
于 2016-04-22T13:43:14.143 回答
3

想法取自?geom_text

ggplot(df, aes(x=reorder(Country, Loss), y=Loss, label = Loss, fill = Taxa)) + 
  geom_bar(position="dodge", stat="identity") +
  geom_text(aes(y = Loss + 0.02), position = position_dodge(0.9), vjust = 0) + 
  coord_flip()

在此处输入图像描述

于 2016-04-22T13:31:23.347 回答