我的数据集是
def contains[A,B](xs: List[A], x: B)(implicit ev: A =:= B) = xs.contains(x)
使用
cvar setting value
var1 min 20
var2 min 5
var3 min 140
var4 min 40
var5 min 600
var1 max 60
var2 max 15
var3 max 180
var4 max 80
var5 max 1200
var1 center 40
var2 center 10
var3 center 160
var4 center 60
var5 center 900
var1 upper 57
var2 upper 13
var3 upper 162
var4 upper 79
var5 upper 1250
var1 lower 20
var2 lower 6
var3 lower 153
var4 lower 40
var5 lower 620
我得到的东西接近我正在寻找的东西。 我想让每个轴单独缩放,从0到相应的最大值,或者每个轴的最小值和最大值。
有人可以给我一个提示吗? 提前致谢 霍尔格
答案 0 :(得分:3)
如果您的意思是希望为每个cvar
缩放值范围,则可以在将数据框传递给ggplot之前执行此操作:
library(dplyr)
daten2 <- daten %>%
group_by(cvar) %>%
# option 1: this scales the values at each varX from 0 to max
mutate(value.scaled = value / max(value)) %>%
# option 2: this scales the values at each varX from min to max
mutate(value.scaled2 = (value - min(value)) / (max(value) - min(value))) %>%
ungroup() %>%
mutate(setting = factor(setting,
levels = c("max", "upper", "center", "lower", "min")))
p1 <- ggplot(data = daten2,
aes(x = factor(cvar), y = value.scaled, group = setting, color = setting)) +
geom_line(size = 1) +
expand_limits(y = 0) +
coord_polar() +
ggtitle("Scaled value range from 0 to max")
p2 <- ggplot(data = daten2,
aes(x = factor(cvar), y = value.scaled2, group = setting, color = setting)) +
geom_line(size = 1) +
coord_polar() +
ggtitle("Scaled value range from min to max")
gridExtra::grid.arrange(p1, p2, nrow = 1)
修改:如果要加入第一个和最后一个cvar
值,可以在x轴的末尾重复var1,并明确设置expand = c(0, 0)
以便最后cvar
值与极坐标下的第一个值连接:
# I'm only demonstrating with one of the two cases above,
# but the same principle applies in either case.
ggplot(data = daten2 %>%
filter(cvar == "var1") %>% # make a copy of the dataset with only var1
mutate(cvar = "var6") %>%
rbind(daten2), # append the copy to the full dataset
aes(x = factor(cvar), y = value.scaled2, group = setting, color = setting)) +
geom_line(size = 1) +
scale_x_discrete(expand = c(0, 0),
breaks = c("var1", "var2", "var3", "var4", "var5")) +
coord_polar() +
ggtitle("Scaled value range from min to max")
如果你想在极坐标下直线,答案here描述了另一种选择:
coord_radar <- function (theta = "x", start = 0, direction = 1) {
theta <- match.arg(theta, c("x", "y"))
r <- if (theta == "x") "y" else "x"
ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start,
direction = sign(direction),
is_linear = function(coord) TRUE)
}
ggplot(data = daten2 %>%
filter(cvar == "var1") %>%
mutate(cvar = "var6") %>%
rbind(daten2),
aes(x = factor(cvar), y = value.scaled2, group = setting, color = setting)) +
geom_line(size = 1) +
scale_x_discrete(expand = c(0, 0),
breaks = c("var1", "var2", "var3", "var4", "var5")) +
coord_radar() +
ggtitle("Scaled value range from min to max")