Why does one radar/spider plot had straight sides and another has curved edges, despite being created with the same data?

56 Views Asked by At

I used the following geom_polygon code to create the radar plot below. In this figure, my four treatment (ES, EO, LO, LS) are at the four polygon corners, and my shapes are specific to 4 streams (Blue, Pink, Orange, Yellow). This code produced a four-sized polygon with straight edges. The same dataset is used to create both figures. Table here:

Substrate Phase vf
Cobble ES 3.6
Pea Gravel ES 4.5
Mix ES 2.1
Sand ES 1.9
Cobble EO 5.6
Pea Gravel EO 6.9
Mix EO 3.2
Sand EO 2.9
Cobble LO 5.9
Pea Gravel LO 7.4
Mix LO 3.4
Sand LO 3.1
Cobble LS 1.8
Pea Gravel LS 2.3
Mix LS 1.1
Sand LS 1.0
ggplot(data=nh420,  aes(x=phase, y=vf, group=substrate, colour=substrate))+ #
  annotate("text", x=4, y=c(2,4,6,8,10), label=c(2,4,6,8,10), hjust=0.5, vjust=1)+
  geom_polygon(size = 1.5, alpha= 0)+
  scale_y_continuous()+
  scale_x_discrete()+
  scale_colour_manual(values= c("#c55a11", "#ab3378", "#4677aa", "#cbbb44"))+
  #scale_fill_manual(values= c("#CA885F", "#B66C98","#4677aa","#cbbb44"))+
  #scale_y_continuous(expand=c(0,0),limits=c(0,10),breaks=seq(0,10,3))+
  #ylab(expression(NH["4"]~""^"+"-N~V["f"]~(m~d^{"-1"})))+
  theme_minimal()+
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        #axis.title.y=element_text(size=12,color="black"),
        #axis.text.y=element_text(size=10,color="black"),
        #axis.text.x=element_text(size=12,color="black"),
        axis.text.x=element_blank(),
        axis.text.y=element_blank())+
  theme(legend.position="none")+
  coord_polar(start = -9.5)

enter image description here

However, when I switch around the x variable and grouping to create the inverse figure, the sides of the polygon are now curved. Why the drastic change in shape? The code for the second image (gray and yellow lines) is below.

ggplot(data=nh420,  aes(x=substrate, y=vf, group=phase, colour=phase))+ #
  annotate("text", x=4, y=0:3, label=0:3, hjust=0.5, vjust=1)+
  geom_polygon(size = 1.5, alpha= 0)+
  scale_y_continuous()+
  scale_x_discrete()+
  scale_colour_manual(values= c("gray75", "yellow", "goldenrod", "gray35"))+
  #scale_fill_manual(values= c("#CA885F", "#B66C98","#4677aa","#cbbb44"))+
  #scale_y_continuous(expand=c(0,0),limits=c(0,10),breaks=seq(0,10,3))+
  #ylab(expression("Areal Uptake,"~italic(U)~(mg~N~m^{"-2"}~h^{"-1"})))+
  theme_minimal()+
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        #axis.title.y=element_text(size=12,color="black"),
        #axis.text.y=element_text(size=10,color="black"),
        #axis.text.x=element_text(size=12,color="black"),
        axis.text.x=element_blank(),
        axis.text.y=element_blank())+
  theme(legend.position="none")+
  coord_polar(start = -9.5)

enter image description here

I do not understand why the shape changes. Any help is appreciated!

1

There are 1 best solutions below

1
Antonio Alegría On

I once had this problem but found a custom function/trick on Erwan Le Pennec blog

Step 1: Create a custom radar coordinate system:

coord_radar <- function (theta = "x", start = - pi / 2, direction = 1) {
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x") "y" else "x"
  ggproto(
    "CordRadar", CoordPolar, theta = theta, r = r, start = start,
    direction = sign(direction),
    is_linear = function(coord) TRUE)
}

Step 2: Add the function to the plot object

ggplot(data=df,  aes(x=Substrate, y=vf, group = Phase, colour =Phase))+ 
  annotate("text", x=4, y=0:3, label=0:3, hjust=0.5, vjust=1)+
  geom_polygon(size = 1.5, alpha= 0)+
  scale_colour_manual(values= c("gray75", "yellow", "goldenrod", "gray35"))+
  scale_y_continuous()+
  scale_x_discrete()+
  theme_minimal()+
  coord_radar(start = -9.5) #NEW and Custom Coordinate System

Step 3: Optional, rearrange the levels to make it easier to read.

df$Substrate <- factor(df$Substrate,
                       levels = c(
                         "Mix",
                         "Pea Gravel",
                         "Cobble",
                         "Sand"
                       ))

Final Result, hope it helps.

radar-plot-custom