Help with adding title to ordered pie chart

Here is what I have so far. But I am not able to add a title.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

contamination <- c('Very High', 'High', 'Intermediate', 'Low')
percentage <- c(50, 20, 5, 25)
label <- c('50% Very high', '20% High', '5% Intermediate', '25% Low')
df <- data.frame(contamination, percentage, label,
                 stringsAsFactors = FALSE)

df %>%
  mutate(contamination = factor(x = contamination,
                                levels = contamination), # ideally you should specify the order explicitly
         label = factor(x = label,
                        levels = label)) %>% # same as above note
  ggplot(mapping = aes(x = 2,
                       y = percentage,
                       fill = label)) +
  geom_bar(width = 1,
           stat = "identity") +
  coord_polar(theta = "y",
              start = 0,
              direction = -1) +
  theme_void() +
  theme(legend.title = element_blank(),
        legend.text = element_text(size = 20,
                                   color = "#58585A")) +
  scale_fill_manual(values = c('blue', 'red', 'black', 'green')) +
  annotate(geom = "text",
           x = 0,
           y = 0,
           label = " ",
           colour = "#009999",
           size = 14)

I have tried all of these and it is not working

What is it you have actually tried that doesn't work?

Assuming your code produces a plot successfully, then if you add + labs(title = "Your Title" or + ggtitle("Your Title") to your code you will get a title.

when i do this it messes up the order of the pie slices

I get this error message "Error in +labs(title = "Your Title") : invalid argument to unary operator"

Please make sure there is a ggplot object on the left side of the + symbol...

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.