-1

I am trying to create a circular bar chart for simple data. R Graph Gallery has a nice reference for it: https://www.r-graph-gallery.com/296-add-labels-to-circular-barplot.html.

My is my dataframe structure (from dput(data)):

structure(list(month = c("Jan", "Feb", "Mar", "Apr", "Dec"), 
    n = c(333, 557, 98, 545, 654), id = 1:5), class = "data.frame", row.names = c(NA, 
-5L))

I cannot dodge the bars properly. I would like to dodge the bars using another categorical variable called: paystatus, which assumes three values: early, ontime and late.

Thank you.

rodolfoksveiga
  • 1,181
  • 4
  • 17
  • (1) Please make your example reproducible (you can use dput()). (2) Show what you've tried. – s_baldur Sep 09 '20 at 14:53
  • [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example. We don't have any of your code in order to help you debug it, we can't see what you're looking at, and we don't know what the "proper results" are or how your results differ from that – camille Sep 09 '20 at 14:59

2 Answers2

0

something like

dataframe %>%
   ggplot(aes(x = id, y = n) + 
   geom_bar() +
   coord_polar()

but without knowing exactly what you're going for...

Andrew McCartney
  • 191
  • 2
  • 10
0

Welcome to Stack Overflow Surbhi Mishra.

I've tried to reproduce the example from R Graph Gallery that you've shared with us. But next time try to elaborate a little better on it. It helps us to help you... :)

Let me cut to the chase. I couldn't use paystatus because you haven't shared the data, but I think you may want something like this:

# setup environment
library(tidyverse)
# create dataframe
data = data.frame(
  month = c('Jan', 'Feb', 'Mar', 'Apr', 'Dec'),
  n = c(333, 557, 98, 545, 654),
  id = 1:5
)
# define labels
nrows = nrow(data)
angle =  90 - 360*(data$id - 0.5)/nrows
label = data
label$hjust = ifelse(angle < -90, 1, 0)
label$angle = ifelse(angle < -90, angle + 180, angle)
# plot
ggplot(data, aes(x = as.factor(id), y = n)) +
  geom_bar(stat = "identity", fill = alpha("skyblue", 0.7)) +
  ylim(-200, 700) +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1, 4), "cm")) +
  coord_polar(start = 0) +
  geom_text(data = label, aes(x = id, y = n + 10, label = month, hjust = hjust),
            color = "black", fontface = "bold", alpha = 0.6, size = 4,
            angle = label$angle, inherit.aes = FALSE)

Here is the output:

enter image description here

Please, let us know if that is what you're looking for.

rodolfoksveiga
  • 1,181
  • 4
  • 17
  • Thank you !! This worked with a little tweak..and yes I will be more descriptive :) Can you pls explain how do we set the limits for ylim ?..(i understand the negative value). There are 32 rows in my data and with ylim(-2000,5000), getting a message - removed 10 rows with missing values (and there are no missing values in the dateset) – Surbhi Mishra Sep 09 '20 at 19:12
  • no worries @SurbhiMishra! by the way, if my solution helped you, give it a like (upvote), it also helps me. about your question, you probably have values of `data$n` higher than 5000, i think changing `ylim(-2002, 5000)` to `ylim(-2000, max(df$n)` will do the trick, doesn't it? – rodolfoksveiga Sep 09 '20 at 19:33