Scaling the axes in a polar plot of ggplot individually?

筅森魡賤 提交于 2019-12-07 22:32:23

问题


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


回答1:


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)

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

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



来源:https://stackoverflow.com/questions/46287409/scaling-the-axes-in-a-polar-plot-of-ggplot-individually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!