Ecg
Ecg

Reputation: 942

Can you combine a geom_bar pie over a geom_point plot in R?

Is there a way to plot geom_pie over a geom_point plot? with a shared Variable (here: Location)

I have seen people do pies over maps plotting pie graphs on map in ggplot

But I was wondering if there is some package or function which allows you to do it?

Here is my data and two current plots:

data_point <- data.frame(
  Location=(1:7),
  Var1=c(13,8, 6,5,4,2,1),
  Var2 = c(7,8,9,10,11,12,13))

data_pie <- data.frame(
  Location=rep(1:7,5),
  Group= c(rep("A",7),rep("B",7),rep("C",7),rep("D",7),rep("E",7)),
  value=c(45.59,63.56,66.47,58.60,67.28,44.45,9.22,0.00,0.00,0.00,0.00,0.00,4.14,37.81,0.00,0.00,0.00,0.04,
0.03,25.15,34.58,52.86,35.61,24.66,26.13,18.98,12.71,6.61,2,1,9,15,14,14,12))

a<-ggplot(data_point, aes(Var1, Var2, label=1:7)) + 
  geom_point(size = 3, stroke = 2)+
  theme_classic()
b<- ggplot(data_pie, aes(x="", y=value, fill=Group)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0) +
  facet_wrap(~Location)+ scale_fill_brewer(palette="Dark2")
ggarrange(a,b, nrow = 1)

enter image description here

Desired plot: enter image description here

Upvotes: 2

Views: 487

Answers (1)

tamtam
tamtam

Reputation: 3671

Here is an idea with geom_scatterpie from the scatterpie package:

Join Data

df <- data_pie %>% 
  pivot_wider(id_cols = Location,
              names_from = Group, 
              values_from =  value) %>% 
  left_join(data_point, by = "Location")

Code

library(scatterpie)

ggplot() +
  geom_scatterpie(data = df, aes(x = Var1, y = Var2,
                      group = Location), 
                  cols = c("A", "B", "C", "D", "E"))

Plot enter image description here

Upvotes: 2

Related Questions