Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate x-axis text when using coord_polar()

Tags:

r

ggplot2

I have a scatter-plot with many values on a polar axis - coord_polar(). The resulting graph has crowded text because the text is always aligned with the bottom of the page.

Is it possible to place the text so that it is printed radially along the polar x-axis?


Edited to provide an example:

qplot(data=presidential, name,end) + coord_polar()

In the presidential case I would like to see the presidential names angled to align with the axis/spoke they are on. Below is an example of the graph I am working on where the x axis is categorical and the y-axis is a continuous variable (similar to the example).

enter image description here

like image 643
zach Avatar asked Oct 20 '11 01:10

zach


People also ask

How do you rotate text on X-axis?

To rotate x-axis text labels, we use “axis. text. x” as argument to theme() function. And we specify “element_text(angle = 90)” to rotate the x-axis text by an angle 90 degree.

What is the coordinate system of a pie chart?

The polar coordinate system is most commonly used for pie charts, which are a stacked bar chart in polar coordinates.


3 Answers

I understand this is an old topic, but I found a better solution for this problem, inspired from baptise's comment:

ggplot(data, aes(x=someId, y=someValue)) +
  geom_point() + 
  coord_polar() +
  theme(axis.text.x = element_text(
    angle= -90 - 360 / length(unique(data$someId)) * seq_along(data$someId)
    )
  )
like image 107
Yoplait Avatar answered Sep 27 '22 17:09

Yoplait


The answer by Yoplait helps a lot, but still does not solve the issue of having to read upside down in half of the graph. An extention of the idea that this poster proposes is as follows.

First prepare the angle vector, making sure we split the graph in two:

sequence_length = length(unique(data$someId))
first_sequence = c(1:(sequence_length%/%2)) 
second_sequence = c((sequence_length%/%2+1):sequence_length) 
first_angles = c(90 - 180/length(first_sequence) * first_sequence)
second_angles = c(-90 - 180/length(second_sequence) * second_sequence)

And now we can append the angle vectors to make the actual graph:

ggplot(data, aes(x=someId, y=someValue)) +
  geom_point() + 
  coord_polar() +
  theme(axis.text.x = element_text(
    angle= c(first_angles,second_angles)
    )
  )
like image 37
FvD Avatar answered Sep 27 '22 17:09

FvD


here is a not elegant example of the coordinate:

CoordPolar2 <- proto(CoordPolar, {
  objname <- "polar2"
  guide_foreground <- function(., details, theme) {
    theta <- .$theta_rescale(details$theta.major, details)
    labels <- details$theta.labels

    # Combine the two ends of the scale if they are close
    theta <- theta[!is.na(theta)]
    ends_apart <- (theta[length(theta)] - theta[1]) %% (2*pi)
    if (ends_apart < 0.05) {
      n <- length(labels)
      if (is.expression(labels)) {
        combined <- substitute(paste(a, "/", b), 
          list(a = labels[[1]], b = labels[[n]]))
      } else {
        combined <- paste(labels[1], labels[n], sep="/")
      }
      labels[[n]] <- combined
      labels <- labels[-1]
      theta <- theta[-1]
    }

    grobTree(
      if (length(labels) > 0) {
        lab <- theme_render(
          theme, "axis.text.x", 
          labels, 0.45 * sin(theta) + 0.5, 0.45 * cos(theta) + 0.5,
          hjust = 0.5, vjust = 0.5,
          default.units="native"
        )
        lab$rot <- (pi/2 - theta) / pi * 180
        lab
      },
      theme_render(theme, "panel.border")
    )
  }
})

coord_polar2 <- CoordPolar2$build_accessor()

p <- qplot(data=presidential, name,end) + coord_polar2()
print(p)

enter image description here

like image 20
kohske Avatar answered Sep 27 '22 18:09

kohske