Question

How might the angle of x-axis labels follow the same angle as the outer x-axis using the coor_polar projection in ggplot? This is similar to rotate x-axis text in ggplot2 when using coord_polar(), but I don't understand the math well enough to adapt it. I found a trial-and-error solution below that sort of works, where angle = c(c(1:3)*c(-14,-22.3,-22),-90,c(3:1)*c(22,22.3,14),c(1:3)*c(-14,-22.3,-22),90,c(3:1)*c(22,22.3,14)). It would also be okay for the x-axis labels to rotate in the same direction all the way around. If all else fails, I might give up on rotating the labels and just add a second legend, as in Two legends for polar ggplot (with one customized). Thanks for the help!

require(ggplot2)    
df.test <- data.frame(Names=c("name01", "name02", "name03", "name04", "name05", "name06",    "name07", "name08", "name09", "name10", "name11", "name12", "name13", "name14"),Values=rep(1,24))
p <- ggplot(df.test, aes(Names, fill=Values))
p + coord_polar(theta="x", direction=1) +
geom_bar(stat="bin", colour="gray", alpha=.7) +
theme(axis.text.x = element_text(angle = c(c(1:3)*c(-14,-22.3,-22),-90,c(3:1)*c(22,22.3,14),c(1:3)*c(-14,-22.3,-22),90,c(3:1)*c(22,22.3,14))))
Était-ce utile?

La solution

I'm not exactly clear what your goal is but see if this answers the question:

+ theme(axis.text.x = element_text(angle = 
            360/(2*pi)*rev( seq( pi/14, 2*pi-pi/14, len=14))))

That would make the names "tangential" to the splits. If you wanted them "perpendicular" (as in the illustration in the linked answer) you just add pi/2 radians to the angle. (Didn't we all take geometry in high school?)

+ theme(axis.text.x = element_text(angle = 
            360/(2*pi)*rev( pi/2 + seq( pi/14, 2*pi-pi/14, len=14))))

(BTW: The data argument that you specified had an error. I changed "24" to "14".)

Let me know if the first 7 labels look right, but you want the ones on the LHS of the figure flipped. enter image description here

So you want the bottom 6 rotated by pi radians (=pi*360/2*pi degrees):

+theme(axis.text.x = element_text(angle = 360/(2*pi)*rev( 
                                             seq(pi/14,2*pi-pi/14, len=14))+
  360/(2*pi)*c( rep(0, 4),rep(pi,6), rep(0,4)) )) 
                # the rotation "back" of the lower items

(I really do not understand the decision to use degrees rather than radians.)

Autres conseils

Here's a small example how the solution of @42- could be implemented. Would have been useful for me.

mtcars$gear <- factor(mtcars$gear)
mtcars$cyl <- factor(mtcars$cyl)

ax <- length(unique(mtcars$gear))

ggplot(mtcars, aes(x = gear, y = cyl)) +
  geom_point() +
  coord_polar() +
  theme(
    axis.text.x = element_text(
      angle = 360/(2*pi)*rev(seq(pi/ax,2*pi-pi/ax, len=ax)) + 360/(2*pi)*c(rep(0, round(ax/3)),rep(pi,ax-2*round(ax/3)), rep(0,round(ax/3)))
    )
  ) +
  scale_y_discrete(
    breaks = levels(mtcars$cyl),
    limits = c(" ", levels(mtcars$cyl))
  ) 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top