3

I'm trying to make a spider chart with ggplot2, using coordpolar.

I would like to remove the outermost circle in the grid on order to make the labels look nicer, any ideas?

I have used the mtcars dataset for reproducability. Here is what I have so far:

library(ggplot2) 
library(reshape2)
library(scales)

# Define a new coordinate system 
coord_radar <- function (theta = "x", start = 0, direction = 1) 
{
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x") 
    "y"
  else "x"
  ggproto("CoordRadar", CoordPolar, theta = theta, r = r, start = start, 
          direction = sign(direction),
          is_linear = function(coord) TRUE)
}
is.linear.radar <- function(coord) TRUE 

# rescale all variables to lie between 0 and 1 
scaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))

scaled$model <- rownames(mtcars)    # add model names as a variable 

as.data.frame(melt(scaled,id.vars="model")) -> mtcarsm

mtcarsm2 <- dplyr::filter(mtcarsm, model = model %in% c("Maserati Bora","Volvo 142E"))
my.color = c("red","darkgreen")

mtcarsm2$Make.color <-  ifelse(mtcarsm2$model=="Maserati Bora", "1",
                               ifelse(mtcarsm2$model=="Volvo 142E", "2",
                                      NA  ))

cols <- c("Maserati Bora" = "darkorange","Volvo 142E" = "blue")

library(scales)
library(ggthemes)

scaled <- as.data.frame(lapply(mtcars, ggplot2:::rescale01))
scaled$model <- rownames(mtcars)    # add model names as a variable
# melt the dataframe
mtcarsm <- reshape2::melt(scaled)
# plot it as using the polygon geometry in the polar coordinates
ggplot(mtcarsm2, aes(x = variable, y = value, fill=as.factor(model))) +
  geom_polygon(aes(group = model, color = model), fill = NA, size = 1) +
  coord_polar()+
  xlab("") + ylab("")+theme_minimal()+scale_colour_manual(
       values = c("Maserati Bora" = "darkorange","Volvo 142E" = "deepskyblue4"))+
  theme(panel.background = element_rect(fill = "lightblue",
                                        colour = "lightblue",
                                        size = 0.5, linetype = "solid"),
        panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                        colour = "white"), 
        panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                                        colour = "white"))

Resulting image

enter image description here

jonas
  • 13,559
  • 22
  • 57
  • 75

1 Answers1

-3

Substituting your current panel.grid.major argument with panel.grid = element_blank() seems to work fine with your example.

ggplot(mtcarsm2, aes(x = variable, y = value, fill = as.factor(model))) +
    geom_polygon(aes(group = model, color = model), fill = NA, size = 1) +
    coord_polar() +
    xlab("") + 
    ylab("") +
    theme_minimal() + 
    scale_colour_manual(values = c("Maserati Bora" = "darkorange",
               "Volvo 142E" = "deepskyblue4")) + 
    theme(panel.background = element_rect(
         fill = "lightblue", colour = "lightblue", size = 0.5, 
         linetype = "solid"),
         panel.grid = element_blank(), 
         panel.grid.minor = element_line(
               size = 0.25, linetype = 'solid', colour = "white"))
jesstme
  • 604
  • 2
  • 10
  • 25
  • 2
    When I run this code using the example data, it removes all lines, not just the outermost. I've been unable to achieve what the original poster wanted (remove just the redundant outer circle) – Sean Murphy Nov 15 '18 at 08:56