gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:13:00 24 4
gpt4 key购买 nike

我正在尝试使用 ggplot2 和 coordpolar 制作蜘蛛图。

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

我使用了 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

最佳答案

将您当前的 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"))

关于使用 ggplot2,coord_polar 删除网格中最外层的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35553514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com