Reputation: 1752
I have an issue when using coord_polar()
together with geom_col()
. I have degree values ranging from 0 to <360. Let's say there are in steps of 20, so 0, 20, 40... 340
. If I plot them with coord_polar()
I have two issues:
See this minimal example.
suppressWarnings(library(ggplot2))
df <- data.frame(x = seq(0,359,20),y = 1)
ninety = c(0,90,180,270)
p <- ggplot(df, aes(x,y)) +
geom_col(colour = "black",fill = "grey") +
geom_label(aes(label = x)) +
scale_x_continuous(breaks = ninety) +
geom_vline(xintercept = ninety, colour = "red") +
coord_polar()
p
If I set the x-axis limits, the rotation of the coordinate system is correct, but the column at 0 disappears due to lack of space.
p+scale_x_continuous(breaks = c(0,90,180,270),limits = c(0,360))
#> Scale for 'x' is already present. Adding another scale for 'x', which
#> will replace the existing scale.
#> Warning: Removed 1 rows containing missing values (geom_col).
Created on 2019-05-15 by the reprex package (v0.2.1)
Upvotes: 0
Views: 2359
Reputation: 29085
Since the space occupied by each bar is 20 degrees, you can shift things by half of that in both scales and coordinates:
ggplot(df, aes(x,y)) +
geom_col(colour = "black",fill = "grey") +
geom_label(aes(label = x)) +
scale_x_continuous(breaks = ninety,
limits = c(-10, 350)) + # shift limits by 10 degrees
geom_vline(xintercept = ninety, colour = "red") +
coord_polar(start = -10/360*2*pi) # offset by 10 degrees (converted to radians)
Upvotes: 2
Reputation: 1248
I got it closer to what you want, but it's a bit of a hack so I don't know if it's a great solution.
Code:
df <- data.frame(x = seq(0,359,20),y = 1)
ggplot(df, aes(x+10,y, hjust=1)) +
geom_col(colour = "black",fill = "grey") +
geom_label(aes(x=x+5,label = x)) +
scale_x_continuous(breaks = c(0,90,180,270),limits = c(0,360)) +
coord_polar()
Instead of plotting the geom_cols's at c(0,20,40,...) I'm now plotting them at c(10,30,50,...). I'm plotting the geom_labels at c(5, 15, 25,...).
The label positioning at the bottom of the chart is still not perfect since 180deg is not South.
Upvotes: 1