相关疑难解决方法(0)

如何使用ggplot2为R中的每个条形图放置geom_bar上的标签

我发现了这个,如何在带有ggplot2的R中将标签放在geom_bar上,但它只是将标签(数字)放在一个条上.

这就是说,每个x轴有两个条,怎么做同样的事情?

我的数据和代码如下所示:

dat <- read.table(text = "sample Types Number
sample1 A   3641
sample2 A   3119
sample1 B   15815
sample2 B   12334
sample1 C   2706
sample2 C   3147", header=TRUE)

library(ggplot2)
bar <- ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) + 
  geom_bar(position = 'dodge') + geom_text(aes(label=Number))
Run Code Online (Sandbox Code Playgroud)

然后,我们将得到: 在此输入图像描述

似乎数字文本也定位在"闪避"模式中.我搜索了geom_text手册来查找一些信息,但无法使其正常工作.

建议?

r bar-chart ggplot2

82
推荐指数
2
解决办法
9万
查看次数

将geom_text放在躲避的条形图上

我试图使标题不言自明,但这里 - 数据优先:

dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"), 
    ustanova = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L), .Label = c("srednja škola", "fakultet"), class = "factor"), 
    `(all)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139, 
    33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692, 
    23.9426605504587, 33.2200772200772, 26.9493087557604)), .Names = c("variable", 
"ustanova", "(all)"), row.names = c(NA, 10L), class = c("cast_df", 
"data.frame"), idvars = c("variable", "ustanova"), rdimnames = list(
    structure(list(variable …
Run Code Online (Sandbox Code Playgroud)

plot r bar-chart ggplot2

79
推荐指数
1
解决办法
4万
查看次数

标签 统计

bar-chart ×2

ggplot2 ×2

r ×2

plot ×1