admin管理员组文章数量:1122846
I would like to create an horizontal rancked stacked barchart. The bars should show the sum of "cost1" and "cost2" with one labels at the end of each bar . Below, you can see the code but I am stack with it beceause it is not rancked and the label numbers do not work. I used the pivot_longer function but maybe it is not the correct way to rearrange the dataset. I am sure there is an easy and clear way to do it. Could you please help me?
library(tidyverse)
data_test <- tribble(
~name, ~cost1, ~cost2, ~totalCost,
"John", 10, 40, 50,
"Martin", 21, 35, 56,
"Michael", 15, 80, 95,
"Dave", 28, 40, 68,
"Dan", 18, 35, 53
)
View(data_test)
df <- data_test %>%
pivot_longer(cols = c("cost1", "cost2"),
names_to = "cost",
values_to = "value")
ggplot(df, aes(x=name, y=value, fill = cost)) +
geom_bar(position = "fill", stat = "identity") +
coord_flip()
I would like to create an horizontal rancked stacked barchart. The bars should show the sum of "cost1" and "cost2" with one labels at the end of each bar . Below, you can see the code but I am stack with it beceause it is not rancked and the label numbers do not work. I used the pivot_longer function but maybe it is not the correct way to rearrange the dataset. I am sure there is an easy and clear way to do it. Could you please help me?
library(tidyverse)
data_test <- tribble(
~name, ~cost1, ~cost2, ~totalCost,
"John", 10, 40, 50,
"Martin", 21, 35, 56,
"Michael", 15, 80, 95,
"Dave", 28, 40, 68,
"Dan", 18, 35, 53
)
View(data_test)
df <- data_test %>%
pivot_longer(cols = c("cost1", "cost2"),
names_to = "cost",
values_to = "value")
ggplot(df, aes(x=name, y=value, fill = cost)) +
geom_bar(position = "fill", stat = "identity") +
coord_flip()
Share
Improve this question
asked Nov 21, 2024 at 14:47
paolotroiapaolotroia
434 bronze badges
3
|
1 Answer
Reset to default 0I think you're looking for this:
ggplot(df, aes(y=name, x=value, fill = cost)) +
coord_cartesian(clip = "off") +
geom_bar(position = "stack", stat = "identity") +
geom_text(
aes(label = after_stat(x), group = name),
stat = 'summary', fun = sum, hjust = -0.5
)
本文标签: rHow to build a stacked barchart with two continuous columns in ggplotStack Overflow
版权声明:本文标题:r - How to build a stacked barchart with two continuous columns in ggplot - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309810a1934147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
position="stack"
rather thanposition="fill"
?. That's the default forgeom_col()
. Rather than usinggeom_bar(position = "fill", stat = "identity")
just usegeom_col()
. – MrFlick Commented Nov 21, 2024 at 14:59ggplot(df, aes(x=reorder(name, value), y=value, fill = cost)) + geom_col() + geom_text(aes(label = label_number(scale_cut = cut_short_scale())(value)), size = 3, hjust = -2, color = "black") + coord_flip()
– paolotroia Commented Nov 21, 2024 at 16:11geom_text(aes(label = after_stat(y), group = name), stat = 'summary', fun = sum, hjust=0)
– MrFlick Commented Nov 21, 2024 at 16:40