我有以下数据集。
data <- read.table(text = "value type dimension weight
0.04161712 NODE CAR 25.00
1.42895171 NODE ACTIVE_TRAVEL 25.00
0.00000000 NODE PUBLIC_TRANSPORT 25.00
0.31904447 TRAIN TRAIN 30.00
4.23052201 PLACE DENSITY 25.00
5.75872982 PLACE DIVERSITY 25.00
1.89965811 PLACE DESIGN 25.00
0.00000000 MOTIVATION SECONDARY_SCHOOL 18.75
0.00000000 MOTIVATION HIGHER_EDUCATION 18.75
0.00000000 MOTIVATION WORK 18.75
0.00000000 MOTIVATION INDIVIDUAL_TICKETS 18.75
4.80514639 RIDERSHIP MORNING_PEAK 30.00
5.46293926 EFFORT WALKING_DISTANCE 25.00
6.02066299 EFFORT BIKING_DISTANCE 25.00
0.51790819 EFFORT OTHER 25.00", header = TRUE, stringsAsFactors=FALSE)
在尝试绘制极坐标图时,我运行:
radarplot <- function(data, x, y, width, values = FALSE) {
df <- data
lab <- df$dimension
xlabel <- as.character(substitute(x))
ylabel <- as.character(substitute(y))
x <- as.character(eval(substitute(x), df))
y <- eval(substitute(y), df)
width <- eval(substitute(width), df)
pos <- positions(width)
p <- suppressWarnings(
ggplot2::ggplot() +
ggplot2::geom_bar(ggplot2::aes(x = pos, width = width, y = y, fill = x),
stat = "identity", colour = "black", size =.2) +
ggplot2::scale_x_continuous(label = lab, breaks = pos) +
ggplot2::xlab(xlabel) +
ggplot2::ylab(ylabel) +
ggplot2::guides(fill = ggplot2::guide_legend(title = xlabel))
)
if(values) {
p + ggplot2::geom_text(ggplot2::aes(x = pos, y = 0, label = y, vjust = -0.5))
} else {
p
}
}
positions <- function(width) {
0.5 * (cumsum(width) + cumsum(c(0, width[-length(width)])))
}
但是,当我运行该函数时,轴标签由于某种原因被切断。我已经尝试过调整,并且调整没有成功。
library(ggplot2)
radarplot(dimensions_sel, type, value, weight) +
coord_polar(start = -1.5708, theta = "x") +
theme_gray() +
theme(axis.ticks = element_blank(),
axis.text.x = element_text(size = 6, margin = margin(t = 0, b = 0)),
axis.text = element_blank(),
axis.title = element_blank(),
axis.line = element_blank())
如您所见,理想情况下,文本应该在圆之外,但我无法做到这一点。
请注意,这不是正常的圆形图,因为条的宽度因可变的重量而异。