0

I have the following df

df <- data.frame(genre = c("Thriller", "Horror", "Action"), europe = c(30, 35, 50), asia = c(20, 15, 25), america = c(50, 50, 25))

     genre europe asia america
1 Thriller     30   20      50
2   Horror     35   15      50
3   Action     50   25      25

I would like to get the plot

enter image description here

Does anyone have any suggestions? Thanks in advance :D

2 Answers 2

1

Using pivot_longer to reshape your data:

library(ggplot2)

ggplot(tidyr::pivot_longer(df, -1), aes(genre, value, fill = name)) +
  geom_col(color = "gray20") +
  geom_text(aes(label = paste(value, "%")), fontface = "bold", size = 5,
            position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("#1ca4fc", "#fcad2a", "#595959"), 
                    name = "Continent") +
  theme(text = element_text(face = "bold", size = 18))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

This should work, you can further change colors/ordering however you'd like.

library(reshape2)
df_melt = melt(df, id.vars = "genre")

ggplot(df_melt, aes(x = genre, y = value, fill = variable)) +
  geom_bar(position = "stack", stat = "identity") +
  geom_text(aes(label = paste0(value, "%")), position = position_stack(vjust = 0.5))

Edited to include labels.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.