0
$\begingroup$

I am trying to plot scatter plot of pie charts using ggplot2 and ggforce. Following is my code

library(data.table)
library(ggplot2)
library(ggforce)
library(dplyr)

df <- fread("data_frame_pie_plot.csv", header = TRUE, 
        data.table = FALSE, stringsAsFactors = FALSE)

req_data <- apply(df, 1, function(x){
  geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = 0, r = 0.45, 
               amount = amount, fill = discrete_fill), stat = "pie")})

ggplot(df) + req_data + theme_no_axes()

plot_obtainedplot_obtained Link_to_data Data_frame.
Can I fill the colours manually (discrete(based on discrete_fill column) and continous (based on conti_fill column))? I have tried the following codes.

##================================================================================##
# Code 1
my_colors <- c("red","white","blue")
ggplot(df, fill = sample_mut_colour) + req_data +
  theme_no_axes() + scale_color_manual(values=my_colors)

##================================================================================##
# Code 2
my_colors_1 <- c("red", "white")
my_colors_2 <- c("blue", "white")

req_data <- apply(df, 1, function(x){
  geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = 0, r = 0.45, 
               amount = amount, 
               fill = discrete_fill,
               color = case_when(discrete_fill == 1 ~ my_colors_1,
                                 discrete_fill == 2 ~ my_colors_2)), stat = "pie")})

ggplot(df) + req_data + theme_no_axes() 

##================================================================================##                  
# Code 3
req_data <- apply(df, 1, function(x){
  geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = 0, r = 0.45, 
               amount = amount, 
               fill = case_when(discrete_fill == 1 ~ my_colors_1,
                                 discrete_fill == 2 ~ my_colors_2)), stat = "pie")})

ggplot(df) + req_data + theme_no_axes() 

##================================================================================##             
# Code 4
req_data <- apply(df, 1, function(x){
  geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = 0, r = 0.45, 
               amount = amount, 
               fill = discrete_fill),
           palette = case_when(discrete_fill == 1 ~ "red",
                               discrete_fill == 2 ~ "black",
                               TRUE ~ "white"), stat = "pie")})

ggplot(df) + req_data + theme_no_axes()  

##=================================================================================##

But they do not seem to work. Any idea what is wrong in the code?

$\endgroup$

1 Answer 1

2
$\begingroup$

For a discrete scale your Code 1 example was nearly there. Just use scale_fill_manual instead of scale_color_manual. For more control over how the discrete values map to color values you can name the vector, too.

my_colors <- c("red","white","blue")
ggplot(df) + req_data +
    theme_no_axes() + scale_fill_manual(values=my_colors)
# or, if you want consistent output independent of the order of
# df$discrete_fill:
my_colors <- c("1" = "red", "2" = "white", "dummy" = "blue")
ggplot(df) + req_data +
    theme_no_axes() + scale_fill_manual(values=my_colors)

You can also just use a single call to geom_arc_bar for the whole data frame (rather than a list of call results from each row) and you'll get the same effect.

ggplot(df) +
  geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = 0, r = 0.45, 
               amount = amount, fill = discrete_fill), stat = "pie") +
    theme_no_axes() + scale_fill_manual(values=my_colors)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.