2

my dataset is

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

With

library(ggplot2)
ggplot(data=daten, aes(x=factor(cvar), y=value, group = setting, color = setting)) + 
  geom_line() + 
  coord_polar()

I get something close to what I am looking for. I would like to have each of the axes scaled individually, either from 0 to the respective max or with individual min and max for each.

Could someone give me a hint, please? Thanks in advance Holger

dww
  • 30,425
  • 5
  • 68
  • 111
HvL
  • 31
  • 4

1 Answers1

3

If you mean you want the range of values to be scaled for each cvar, you can do that before passing the data frame to 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)

enter image description here

Edit: If you want to join the first and last cvar values, you can repeat var1 at the end of the x-axis, and explicitly set expand = c(0, 0) so that the last cvar value joins up with the first under polar coordinates:

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

joined circle

And if you want the lines to be straight under polar coordinates, the answer here describes an alternative:

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

pentagon

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • Thanks Z.Lin, I was looking for built-in methods from ggplot or from another library, but your answer and the comment from @PoGibas confirm, that there is no preconfigured way of doing this. I could now suppress the common scale and label each axis with the respective max value plus its name, and close the lines between start and end (how?). I suppose the turning the rounded curves into direct lines would work with geom_step? Thanks again, I voted your answer up. – HvL Sep 19 '17 at 12:28
  • @HvL Have edited my answer. Turning rounded lines straight under polar coordinates has been asked a few times on SO, but some workarounds have expired. The workaround I linked still works. For now. :) – Z.Lin Sep 19 '17 at 13:05