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 Did you mean position="stack" rather than position="fill"?. That's the default for geom_col(). Rather than using geom_bar(position = "fill", stat = "identity") just use geom_col(). – MrFlick Commented Nov 21, 2024 at 14:59
  • Thanks it works! However, I cdo not know how to add just the total cost as label at the end of each bar. ggplot(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:11
  • 2 That part seems like a dup of stackoverflow.com/questions/30656846/… geom_text(aes(label = after_stat(y), group = name), stat = 'summary', fun = sum, hjust=0) – MrFlick Commented Nov 21, 2024 at 16:40
Add a comment  | 

1 Answer 1

Reset to default 0

I 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