简体   繁体   中英

ggplot2: How to show a datapoint in a circular 360 degree bar plot with an angle of 0 degree?

I got a circular 360 degree bar plot in which I manually need to specify the angle of each bar (numeric x-axis). In general the plotting works fine besides for a datapoint having an angle of 0 resp. 360 degree. This row is removed and not plotted, not even with a bar width of 1.

Why is that and how can a bar with a center value of 0 and a bar width of eg 20 be drawn (from -10 resp. 350 degree to 10 degree)?

Thanks!

MWE:

library(tidyverse)

# dummy data
data <- data.frame(
  variable = c(0, 90, 180, 270), 
  value = c(50, 100, 150, 200))

# plot
ggplot(data, aes(x = variable, y = value, fill = factor(variable))) +
  geom_col(width = 20)  + # Even with a widh of 1 the first datapoint is removed from the plot.
  scale_x_continuous(breaks = seq(from=0, to=359, by=10),  limits=c(0, 360)) +
  coord_polar(start = 0)

I can't see an easy general solution to plotting bars that straddle the wrap-around point of polar co-ordinates, but it's easy to shift the limits and start point to get the desired effect:

ggplot(data, aes(x = variable, y = value, fill = factor(variable))) +
  geom_col(width = 20)  + 
  scale_x_continuous(breaks = seq(0, 359, by = 10), limits = c(-10, 350)) +
  coord_polar(start = -pi/18, clip = "off")

在此处输入图片说明

Obviously this would now cause problems if you try to plot bars between 350 and 359 degrees rather than between 0 and 9 degrees, but assuming you don't want any bars overlapping I'm guessing this isn't a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM