How to fill arc bars with chosen colors

70 views
Skip to first unread message

Aurel

unread,
May 19, 2020, 6:21:45 AM5/19/20
to ggplot2
Hi everyone ! I am trying to create a custom figure using ggplot2, and I'm struggling with filling arc bars as I want them to be filled. Any help would be higly appreciated !

Here is my code :

#Data frame for geom_rect :
df
.carres<-data.frame(x1=c(0,0.805,0,0.805),x2=c(0.795,1.6,0.795,1.6),y1=c(0,0,0.505,0.505),y2=c(0.495,0.495,1,1),f=c("brown","lightblue","green","mediumorchid1"))

#Data frame for geom_arc_bar :
arcs
<- data.frame(
     x0
= 0.8, #Coord x du centre de chaque arc
     y0
= 0.5, #Coord y du centre de chaque arc
     start
= c( rep(seq(0, 2*pi, length.out = 5)[-5],2) , seq(0, 2*pi, length.out = 13)[-13]),  #starting coordinates of each arc
     
end = c( rep(seq(0, 2*pi, length.out = 5)[-1],2), seq(0, 2*pi, length.out = 13)[-1]),  #end coordinates of each arc
     r0
= c(rep(0.40,4),rep(0.45,4),rep(0.35,12)), #start arc radius
     r
= c(rep(0.45,4),rep(0.5,4),rep(0.4,12)),  #end arc radius
     fill
=c(rep("white",4),
             
"darkorchid3","cyan4","coral4","darkgreen",
             rep
(c("cadetblue","dodgerblue2","navyblue"),4))
     
)

ggplot
()+
  theme_void
()+  #cleaning graphical window
  geom_rect
(data = df.carres, aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2),fill=df.carres$f,colour="black",alpha=0.6)+  #Create rects
  theme
(legend.position="none")+  #remove legend
  geom_arc_bar
(data = arcs, aes(x0 = x0, y0 = y0, r0 = r0, r = r, start = start,
                               
end = end, fill=fill),n=5000)+
  geom_circle
(aes(x0=0.8,y0=0.5,r=0.35),fill="white",n=10000,size=1)

It works fine to produce the kind of figure I want.

plot_zoom_png.png

However, I can't figure out how to choose the filling colors of the arc bars with the colors I defined in the "fill" column of the "arcs" dataframe.
I tried several options : fill argument out ouf the aesthetics, use scale_fill_manual, but I did not succed in solving that...

What did I miss ?
Thank you very much for your help !

Ista Zahn

unread,
May 19, 2020, 6:59:29 AM5/19/20
to ggplot2
scale_fill_manual is what you are looking for, e.g., 

scale_fill_manual(values = setNames(unique(arcs$fill), unique(arcs$fill)))

Aurel

unread,
May 19, 2020, 9:14:21 AM5/19/20
to ggplot2
Hi Ista ! Thank you very much for your quick answer !

I finally managed to get what I wanted thanks to your help and a few minor modifications of my code.
If anyone deal with the same kind of problem one day, find the working code below (modification are in bold). Had to transform the "fill" column in a factor-like variable with 8 levels, and then define a  vector of correspondance between levels and colors.
Then, the scale_fill_manual function works fine.

#Data frame for geom_rect :
df
.carres<-data.frame(x1=c(0,0.805,0,0.805),x2=c(0.795,1.6,0.795,1.6),y1=c(0,0,0.505,0.505),y2=c(0.495,0.495,1,1),f=c("brown","lightblue","green","mediumorchid1"))

#Data frame for geom_arc_bar :
arcs
<- data.frame(
     x0
= 0.8, #Coord x du centre de chaque arc
     y0
= 0.5, #Coord y du centre de chaque arc
     start
= c( rep(seq(0, 2*pi, length.out = 5)[-5],2) , seq(0, 2*pi, length.out = 13)[-13]),  #starting coordinates of each arc
     
end = c( rep(seq(0, 2*pi, length.out = 5)[-1],2), seq(0, 2*pi, length.out = 13)[-1]),  #end coordinates of each arc
     r0
= c(rep(0.40,4),rep(0.45,4),rep(0.35,12)), #start arc radius
     r
= c(rep(0.45,4),rep(0.5,4),rep(0.4,12)),  #end arc radius

     fill
=c(rep(1,4),2:5,rep(6:8,4))
     )
cols<-c("1"="white","2"="darkorchid3","3"="cyan4","4"="coral4","5"="darkgreen","6"="cadetblue","7"="dodgerblue2","8"="navyblue")

ggplot()+
  theme_void
()+  #cleaning graphical window
  geom_rect
(data = df.carres, aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2),fill=df.carres$f,colour="black",alpha=0.6)+  #Create rects
  theme
(legend.position="none")+  #remove legend
  geom_arc_bar
(data = arcs, aes(x0 = x0, y0 = y0, r0 = r0, r = r, start = start,

                               
end = end, fill=factor(fill)),n=5000)+scale_fill_manual(values = cols)+
  geom_circle
(aes(x0=0.8,y0=0.5,r=0.35),fill="white",n=10000,size=1)

Perhaps there are other ways, but it works.
Thanks again !
Reply all
Reply to author
Forward
0 new messages