Is there any way to modify this pie chart ?
1
0
Entering edit mode
7 months ago
ohtang7 ▴ 40

Hello,

I am just curious if there is any function in R that I can use to create the pie chart graph shown in the figure?

I want to draw lines that connect the assigned labels to each segment of the pie chart, similar to what is depicted in the figure.

Could you please let me know if there is a package or another method to achieve this in R?

Thanks.enter image description here

FYI my code is as

library(ggplot2)
library(readxl)
data2 <- read_excel("Pigeon_RelativeAbundance.xlsx", sheet = "Sheet1")

genus_colors <- c("#FF0000", "#FF7F00", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#8B00FF", "#FF00FF",
                  "#FF4500", "#FF8C00", "#FFD700", "#ADFF2F", "#00FF7F", "#40E0D0", "#00BFFF", "#8A2BE2",
                  "#9932CC", "#FF1493", "#FF69B4", "#FFC0CB", "#FFE4B5", "#008888", "#0CCCCC", "#009900",
                  "#FAFAD2")

# Create pie chart
plot_a <- ggplot(data2, aes(x = "", y = RelativeAbundance, fill = Taxa)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y") +  # Convert to polar coordinate system
  scale_fill_manual(values = genus_colors) +
  theme_void() +  # Remove background and gridlines
  theme(legend.position = "bottom") +  # Move legend to bottom
  guides(fill = guide_legend(title = "Taxa")) +  # Customize legend title
  labs(title = "A")  # Add title

print(plot_a)
pie ggplot R chart • 710 views
ADD COMMENT
1
Entering edit mode
7 months ago
marco.barr ▴ 160

Hi, you can add the labels outside chart using geom_label_repel from ggrepel package. Check out this guide

https://r-charts.com/part-whole/pie-chart-labels-outside-ggplot2/

ADD COMMENT
0
Entering edit mode

Thank you, I've tried but I see only some appear in the chart in wrong segments. Could you help me to solve this how ?

enter image description here

My code is as this.

plot_a <- ggplot(data, aes(x = "", y = Proportion, fill = Taxa)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar(theta = "y") +  # Convert to polar coordinate system
  scale_fill_manual(values = genus_colors) +
  theme_void() +  # Remove background and gridlines
  theme(legend.position = "bottom") +  # Move legend to bottom
  guides(fill = guide_legend(title = "Taxa")) +  # Customize legend title
  labs(title = "A") +  # Add title
  geom_label_repel(aes(label = Taxa), 
                   show.legend = FALSE,
                   box.padding = 0.001, 
                   point.padding = 0.001,
                   point.size = 1,  # 
                   min.segment.length = 0.001, 
                   angle = 45)  # Rotate labels if necessary
ADD REPLY
0
Entering edit mode

Try following the same logic as the guide which adds the labels about halfway through each pie slice value. Check column's name in script because i don''t know you data :

library(ggplot2)
library(ggrepel)
library(tidyverse)

data2 <- read_excel("Pigeon_RelativeAbundance.xlsx", sheet = "Sheet1")

genus_colors <- c("#FF0000", "#FF7F00", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#8B00FF", "#FF00FF",
              "#FF4500", "#FF8C00", "#FFD700", "#ADFF2F", "#00FF7F", "#40E0D0", "#00BFFF", "#8A2BE2",
              "#9932CC", "#FF1493", "#FF69B4", "#FFC0CB", "#FFE4B5", "#008888", "#0CCCCC", "#009900",
              "#FAFAD2")

df2 <- data2 %>% mutate(csum = rev(cumsum(rev(RelativeAbundance))), 
     pos = RelativeAbundance/2 + lead(csum, 1),
     pos = if_else(is.na(pos), RelativeAbundance/2, pos))

plot_a <- ggplot(data2, aes(x = "", y = RelativeAbundance, fill = fct_inorder(Taxa))) +
geom_bar(width = 1, stat = "identity") +
coord_polar(theta = "y") +  
scale_fill_manual(values = genus_colors) +
theme_void() +  
theme(legend.position = "bottom") +  
guides(fill = guide_legend(title = "Taxa")) + 
labs(title = "A") +
geom_label_repel(data = df2,
               aes(y = pos, label = Taxa),
               size = 4.5, nudge_x = 1, show.legend = FALSE)
ADD REPLY

Login before adding your answer.

Traffic: 1896 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6