我正在尝试使用GGplot创建一个漂亮的蜘蛛图,但有一些东西我仍然需要修复 .
-
如果可能的话,我想在图中为圆圈着色
-
如果可能的话,还要制作一个漂亮的网格,以便标记看起来不错
-
如果可能,在情节中将位置mpg设置为"12 O CLOCK"
使绘图更好的任何其他技巧都非常受欢迎 . 我已经使用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"))
结果图片