ggplot2 中有多个 tags/info 的饼图
Pie chart with multiple tags/info in ggplot2
我想在 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 中创建):
这应该可以帮助您入门 - 您可能需要 fiddle 稍微调整一下标签的位置。我参考了 this question 作为开始。
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 创建
这几乎就是您要的,带有市值和公司名称的标签
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"))
我想在 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 中创建):
这应该可以帮助您入门 - 您可能需要 fiddle 稍微调整一下标签的位置。我参考了 this question 作为开始。
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 创建这几乎就是您要的,带有市值和公司名称的标签
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"))