Achak Achak - 2 months ago 17
R Question

ggplot ordering data within circular plot

Good afternoon,

I am wondering if someone could point me out to the right direction to solve a circular plot issue. I have some sample data and I am trying to produce something similar to the following picture which shows simply 8 weeks of directional data with risk values ranging from very low risk (represented as "0" to high risk represented as "4" in the data set.

this is done in paint and I am trying to create something with R. the following is 8 week data plot. The plot simply shows for each 15 degree direction lowest to highest weekly risk. enter image description here
the example represent following colours:

white= very low, green= low, yellow= medium, orange= high and red= very high

I have tried to reproduce similar design like this but struggling with sorting it out to show lowest to highest irrespective of week number for each 15 degree section as shown in the figure above.

my code so far get me this:

enter image description here

what I am struggling to fix is:


  1. the order of reporting for each 15 degree from very high to very low without any gap on the data as the picture (for example if 2 weeks data available only present up to 2 weeks- see 0-15 degree within the figure)

  2. the order of legend from very low to very high



I have tried scale_fill_manual but I think I am not using this right.

my codes are so far:

ggplot(dat, aes(Arc, "", fill=Risk)) +
geom_bar(width=14.5, stat='identity', colour=("grey90")) +
scale_fill_manual(values = c("Red", "orange","green","yellow","white")) +
coord_polar(start= 0)+
labs(x = '', y = '') +

theme_bw() +

theme(panel.border = element_blank(),
legend.key = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank(),
legend.position="bottom")


and my sample input data is:

dput(dat)
structure(list(Arc = c(7.5, 22.5, 37.5, 52.5, 67.5, 82.5, 97.5,
112.5, 127.5, 142.5, 157.5, 172.5, 187.5, 202.5, 217.5, 232.5,
247.5, 262.5, 277.5, 292.5, 307.5, 322.5, 337.5, 352.5, 7.5,
22.5, 37.5, 52.5, 67.5, 82.5, 97.5, 112.5, 127.5, 142.5, 157.5,
172.5, 187.5, 202.5, 217.5, 232.5, 247.5, 262.5, 277.5, 292.5,
307.5, 322.5, 337.5, 352.5, 7.5, 22.5, 37.5, 52.5, 67.5, 82.5,
97.5, 112.5, 127.5, 142.5, 157.5, 172.5, 187.5, 202.5, 217.5,
232.5, 247.5, 262.5, 277.5, 292.5, 307.5, 322.5, 337.5, 352.5,
7.5, 22.5, 37.5, 52.5, 67.5, 82.5, 97.5, 112.5, 127.5, 142.5,
157.5, 172.5, 187.5, 202.5, 217.5, 232.5, 247.5, 262.5, 277.5,
292.5, 307.5, 322.5, 337.5, 352.5, 7.5, 22.5, 37.5, 52.5, 67.5,
82.5, 97.5, 112.5, 127.5, 142.5, 157.5, 172.5, 187.5, 202.5,
217.5, 232.5, 247.5, 262.5, 277.5, 292.5, 307.5, 322.5, 337.5,
352.5, 7.5, 22.5, 37.5, 52.5, 67.5, 82.5, 97.5, 112.5, 127.5,
142.5, 157.5, 172.5, 187.5, 202.5, 217.5, 232.5, 247.5, 262.5,
277.5, 292.5, 307.5, 322.5, 337.5, 352.5, 7.5, 22.5, 37.5, 52.5,
67.5, 82.5, 97.5, 112.5, 127.5, 142.5, 157.5, 172.5, 187.5, 202.5,
217.5, 232.5, 247.5, 262.5, 277.5, 292.5, 307.5, 322.5, 337.5,
352.5, 7.5, 22.5, 37.5, 52.5, 67.5, 82.5, 97.5, 112.5, 127.5,
142.5, 157.5, 172.5, 187.5, 202.5, 217.5, 232.5, 247.5, 262.5,
277.5, 292.5, 307.5, 322.5, 337.5, 352.5), RiskV = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 3L,
3L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 2L, 2L, 2L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 2L, 3L, 3L, 3L,
3L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L,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7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L),
Risk = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L,
3L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 3L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 3L, 1L, 1L, 4L, 4L,
4L, 4L, 1L, 1L, 3L, 3L, 3L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L,
3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 3L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 1L,
3L, 2L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 1L, 1L, 1L, 4L, 4L, 4L, 1L, 1L, 3L, 2L, 2L, 5L, 2L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 2L, 3L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 2L, 5L, 5L, 2L, 2L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 3L), .Label = c("High", "Low", "Medium", "Very High",
"Very Low"), class = "factor")), .Names = c("Arc", "RiskV",
"week", "Risk"), class = "data.frame", row.names = c(NA, -192L
))


if anyone could point me where I am going wrong that would be great, thanks in advance

Answer

To have a nice display I remove the values which have a "Very Low" risk

newdata=dat%>%filter(RiskV>=1)

I change the levels so that the "Very Low" level won't mess with the legend.

newdata$Risk<-factor(newdata$Risk,levels=c("Very High","High","Medium","Low"))

And plot:

ggplot(newdata, aes(Arc)) +
  geom_bar(width=14.5, stat='identity',position = "stack",colour=("grey90"),aes(y=1,fill=Risk)) + 
  coord_polar()+
scale_fill_manual(values = c("Red", "orange","green","yellow"))