r - 分组条形图中的 ggplot 标签条

标签 r ggplot2

我尝试绘制的数据结构如下:

      Year                        Country                           Count   
 1:   2010 St. Vincent and the Grenadines                               0
 2:   1970                        Ukraine                               0
 3:   1980                          Yemen                               1
 4:   1970                        Romania                               0
 5:   1950                         Cyprus                               0
 6:   1950                    Netherlands                               0
 7:   1980                     Mauritania                               0
 8:   1980                          Niger                               0
 9:   2010                        Grenada                               2
10:   1970                         Israel                               6
11:   1990                       Suriname                               0
12:   1990                      Singapore                               1
13:   1960                         Russia                               0
14:   1970                       Barbados                               0
15:   1950                         Panama                               0
16:   2010                           Mali                               3
17:   1980                         Greece                              11
18:   2010                      Venezuela                              15
19:   2000                         Malawi                               9
20:   2000                        Jamaica                              34
21:   1970                         Angola                               0
22:   1990                        Lebanon                               0
23:   1980       Central African Republic                               0
24:   1950                 United Kingdom                               1
25:   2010                        Iceland                              26

我用它创建了以下分组条形图: Grouped Bar Graph

我需要国家/地区标签与其相应的栏对齐。即使咨询后herehere并按照链接中的建议使用position =position_dodge(width= 0.9),标签似乎没有对齐。我还尝试了不同的宽度值。

以下是创建上述绘图的代码:

> p<-ggplot(x[which(x$Count>0)], aes(Year, Count, label=Country)) + geom_bar(aes(fill = Country), position = "dodge", stat="identity")
> p+ theme(legend.position="none")+scale_x_discrete(limits=unique(x$Year))+geom_text(position = position_dodge(width= 0.9), aes(y=Count+10), angle=90)

最佳答案

fill = Country 添加到 geom_text 并明确指定躲避宽度以确保条形图和标签对齐:

library(data.table)
library(ggplot2)

# Fictional sample data
x <- data.table(Year = c(2010,1970,1980,1970,1950,1950,1980,1980,2010), 
                Country = c("St. Vincent and the Grenadines", "Ukraine", "Yemen", "Romania", "Cyprus", "Netherlands",
                            "Mauritania", "Niger", "Grenada"), Count = c(5,2,1,4,7,6,4,1,2))

p <- ggplot(x[which(x$Count>0)], aes(Year, Count)) + geom_bar(aes(fill = Country), position = position_dodge(9), stat="identity")
p + theme(legend.position="none") + scale_x_discrete(limits=unique(x$Year)) + geom_text(position = position_dodge(width= 9), aes(y=Count+0.25, fill=Country, label=Country, hjust=0), angle=90)

(注:我还用hjust调整了位置)

enter image description here

关于r - 分组条形图中的 ggplot 标签条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37587288/

相关文章:

在R中不同时间复制向量中的每个元素

r - 如何在 gganimate 中取消按字母顺序排列帧顺序?

r - 在 ggplot2 中设置切面的绝对大小

r - 使用 cowplot 中的 plot_grid 排列绘图时,较长的绘图标签向右移动

r - 如何防止线延伸到整个图形

原位替换数据框中列中的值

R:如何根据因子水平计算差异?

r - caret 包中的 train() 返回有关名称和 gsub 的错误

javascript - 将 javascript (d3.js) 绑定(bind)到 shiny

r - 如何将效果大小添加到 t 检验的 ggplot 条形图? (例如 Cohen 的 d 或 Hedges 的 g)