2

I have the following dataset:

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)

As I'm trying to plot a polar graph, I run:

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)])))
}

However, when I run the function, the axis labels get cut off for some reason. I've tried to adjust it with vjust and `hjust with no success.

library(ggplot2)    
radarplot(data, 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())

As you can see, ideally the text should be outside of the circle, but I can't make that happen.

graph

Please note that this is not a normal circular plot as the bar widths are all different as they depend on the variable weight.

Quinten
  • 35,235
  • 5
  • 20
  • 53
FilipeTeixeira
  • 1,100
  • 2
  • 9
  • 29
  • 1
    Do you mean that the axis labels are postioned on the circumference instead of outside it? The legend, the coloured boxes on the right, seems fine to me. – MrGumble Sep 05 '18 at 07:37
  • @MrGumble I mean the axis labels indeed. I'll correct it on the original post. – FilipeTeixeira Sep 05 '18 at 07:49
  • 3
    Have you tried to set `clip = "off"` in the `coord_polar` function? – Roman Sep 05 '18 at 07:57
  • 1
    @Jimbou that doesn't bring the axis labels out of the circle. It only stops the main square/frame from clipping it. But it was a good tip still. – FilipeTeixeira Sep 05 '18 at 08:13
  • your example data causes problems with read.table because of the spaces in your dimension column. Could you kindly consider providing better readable data? Thanks – tjebo Sep 05 '18 at 10:42
  • @Tjebo There was no need to comment twice about the same thing. Your example misses as well some details which in case I wouldn't know how to create a reproducible dataset, it would only make it more confusing. However, I've included the necessary information for a proper reproducible example. I hope that now you'll be able to contribute for the answer. Thank you for your help. – FilipeTeixeira Sep 05 '18 at 12:33
  • i haven’t commented about the same thing - please read my comments carefully. In my second comment I was referring to the missing assignment of objects which you are using in you function calls. – tjebo Sep 05 '18 at 13:56
  • 1
    https://stackoverflow.com/questions/10788436/ggplot2-polar-plot-axis-label-location - seems to be a known problem. might need a workaround, e.g., adding a geom text layer with your annotations. – tjebo Sep 05 '18 at 14:28
  • @Tjebo would you mind to remove the down vote? You did it because the data wasn't reproducible, now that it is, there's no reason for the down vote to be there. – FilipeTeixeira Sep 10 '18 at 06:54
  • I am not at my computer but can see here on my phone that the code is still not yet reproducible. as mentioned in my previous comment , you are using objects in your radar plot function which you haven’t defined. Please try your code in an empty(!) fresh session, and you will see. Let me know when you have made your code safe for fresh sessions and I am very happy to remove the down vote then. – tjebo Sep 10 '18 at 07:23

1 Answers1

0

I'm turning Roman's comment into an answer, since it worked wonders for me:

Set the argument clip = "off" within the coord_polar function. Before doing this: enter image description here

My 270 was cut off by the plot window extents. With clip = "off": enter image description here

270 is no longer cut off. These big numbers do overlay some of my plotted data, which I fixed by adding the following code within the theme() function:
axis.text.x = element_text(face = "bold", color = "black", size = 6): enter image description here

For longer labels like in OP's situation, one could also add a line skip in your custom label names:
xlabel <- c(..., "BIKING \nDISTANCE", ...)

bt3
  • 156
  • 1
  • 10