Pie chart with multiple tags/info in ggplot2

Viewed 153

I want to make a pie-chart in R visualizing four companies by their name and market cap. Preferably I want to do this using the ggplot2 package since I have used this for graphs and histograms etc previously. Below is an example of my data frame and my desired output.

This is an example of a representable data frame for my data:

  Companies <- data.frame(
  Company = c("Company1", "Company2", "Company3", "Company4"),
  Market_cap = c(500, 200, 150, 90),
  Industry = c("Industry 1", "Industry 2", "Industry 3", "Industry 4"))

Desired output (created in Excel):

enter image description here

2 Answers

This is pretty much what you've asked for, with labels for market cap and company name

library(ggplot2)
library(dplyr)
Companies %>%
  mutate(Perc = Market_cap / sum(Market_cap)) %>%
  ggplot(aes(x = "", y = Perc, fill = Industry)) +
  geom_bar(stat = "identity", width = 1, color = "black") +
  coord_polar("y", start = 0) +
  geom_text(aes(label = paste0(Market_cap, "\n", Company)), position = position_stack(vjust = 0.5)) +
  labs(x = NULL, y = NULL, fill = NULL) +
  theme_classic() +
  theme(
    axis.line = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    legend.position = "bottom"
  ) +
  scale_fill_manual(values=c("red", "blue", "green", "purple"))

enter image description here

This should get you started - you'll likely want to fiddle with the placement of the labels a bit. I referenced this question for a start on that.

Companies <- data.frame(
  Company = c("Company1", "Company2", "Company3", "Company4"),
  Market_cap = c(500, 200, 150, 90),
  Industry = c("Industry 1", "Industry 2", "Industry 3", "Industry 4"))

Companies$pos <- cumsum(Companies$Market_cap) - Companies$Market_cap/4
Companies$lab <- paste0(Companies$Company, " - ", Companies$Market_cap)

library(ggplot2)

ggplot(Companies, aes(factor(1), Market_cap, , fill = reorder(Industry, Market_cap))) +
  geom_bar(width = 1, stat = "identity") +
  geom_text(aes(x= factor(1), y=pos, label = lab)) +
  coord_polar("y", start = 0) +
  ylab("") +
  xlab("") +
  theme_bw() +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        panel.grid = element_blank(),
        axis.text.y=element_blank(),
        axis.ticks = element_blank())

Created on 2020-05-24 by the reprex package (v0.3.0)

Related