使用ggplot2,coord_polar删除网格中最外面的圆圈

时间:2016-02-22 12:19:53

标签: r ggplot2

我试图使用coordpolar制作带有ggplot2的蜘蛛图表。

我想删除网格中最外面的圆圈,以使标签看起来更好,有什么想法吗?

我已将mtcars数据集用于再现性。以下是我到目前为止的情况:

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"))

产生的图像

enter image description here

1 个答案:

答案 0 :(得分:-1)

panel.grid.major替换当前的panel.grid = element_blank()参数似乎与您的示例一致。

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"))