ggplot2中具有多个标签/信息的饼图

问题描述 投票:1回答:2

我想在R中制作一个饼图,以其名称和市值可视化四家公司。最好是我想使用ggplot2软件包来做到这一点,因为我以前已经将它用于图形和直方图等。下面是我的数据框和所需输出的示例。

这是我的数据可表示的数据帧的示例:

  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"))

所需的输出(在Excel中创建):

enter image description here

r dataframe ggplot2 pie-chart
2个回答
2
投票
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


0
投票
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())

“”

reprex package(v0.3.0)在2020-05-24创建

© www.soinside.com 2019 - 2024. All rights reserved.