 # Pie chart donut help

Hi everyone,

I'm trying to make a donut graph similar to this image: However, what I would like to do is make the outer circle a different treatment group than the inner one comparing the change in microbial groups between treatments. Specifically, here is my code that I've made another graph for to get the one circle;however, I would like to have a second one on the inside. I have looked at other forms but the code is a bit advanced for me. Does anyone know how to add the second one and could help me with the code. This is as far as I have gotten listed below:

data1 <- data.frame(
category=c("Bacteria1", "Bacteria2", "Bacteria3", "Bacteria4"),
count=c(63.57, 0.53, 0.13,187.93) #pulled from the spreadsheet
)

# Compute percentages

data1\$fraction = data1\$count / sum(data1\$count)

# Compute the cumulative percentages (top of each rectangle)

data1\$ymax = cumsum(data1\$fraction)

# Compute the bottom of each rectangle

data1\$ymin = c(0, head(data1\$ymax, n=-1))

#comput label position
data1\$labelPosition <- (data1\$ymax + data1\$ymin) / 2

# Compute a good label

data1\$label <- paste0(data1\$category, "\n value: ", data1\$count)

# Make the plot for the low nutrient and shade consumption rates

ggplot(data1, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=category)) +
geom_rect() +
theme_classic()+
scale_fill_manual(values = my_colors) +
ggtitle("Environmental Changes alter Bacteria") +
theme_void()+
coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
#xlim(c(2, 4)) # Try to remove that to see how to make a pie chart
xlim(c(-1, 4))

Hi, I'm not sure about label positioning, but does this get you closer?

``````library(ggplot2)

data1 <- data.frame(
category=c("Bacteria1", "Bacteria2", "Bacteria3", "Bacteria4"),
count=c(63.57, 0.53, 0.13,187.93, 187.93, 0.13, 0.53, 63.57),
treatment = rep(c(1, 2), c(4,4))
)
# Compute percentages
data1\$fraction = data1\$count / sum(data1\$count)

# Compute a good label
data1\$label <- paste0(data1\$category, "\n value: ", data1\$count)

# Make the plot for the low nutrient and shade consumption rates

ggplot(data1, aes(x = treatment, y = fraction, fill=category)) +
geom_col() +
theme_classic()+
# scale_fill_manual(values = my_colors) +
ggtitle("Environmental Changes alter Bacteria") +
theme_void()+
coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
#xlim(c(2, 4)) # Try to remove that to see how to make a pie chart
geom_text(aes(label = category)) +
xlim(c(-1, 4))
``````
1 Like

Hi kstachelek,

Yes! It does get me closer. But, now the problem is that I have multiple data frames. I also have one for a second treatment with bacteria 1,2,3,4. Do you think you could help me put the data1 on the outside ring and then the data2 on the outside? Here is my code for the data2 for that. Essentially, data2 is just a different treatment and I want to show the percent change between treatment 1 and treatment 2:
data <- data.frame(
category=c("Bacteria1", "Bacteria2", "Bacteria3", "Bacteria4"),
count=c(70.62, 0.56, 0.21, 257.75) #pulled from the spreadsheet
)

# Compute percentages

data\$fraction = data\$count / sum(data\$count)

# Compute the cumulative percentages (top of each rectangle)

data\$ymax = cumsum(data\$fraction)

# Compute the bottom of each rectangle

data\$ymin = c(0, head(data\$ymax, n=-1))

#comput label position
data\$labelPosition <- (data\$ymax + data\$ymin) / 2

# Compute a good label

data\$label <- paste0(data\$category, "\n value: ", data\$count)

#making the plot
ggplot(data, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=category)) +
geom_rect() +
theme_classic()+
scale_fill_manual(values = my_colors) +
ggtitle("Environmental Changes alter Bacteria") +
theme_void()+
coord_polar(theta="y") + # Try to remove that to understand how the chart is built initially
#xlim(c(2, 4)) # Try to remove that to see how to make a pie chart
xlim(c(-1, 4))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.