Circular bar plot

Viewed 2596

I'm trying to adjust https://www.r-graph-gallery.com/295-basic-circular-barplot/ to my needs but I don't get it.

The original code is:

# Libraries
library(tidyverse)

# Create dataset
data=data.frame(
  id=seq(1,60),
  individual=paste( "Mister ", seq(1,60), sep=""),
  value=sample( seq(10,100), 60, replace=T)
)

# Make the plot
p = ggplot(data, aes(x=as.factor(id), y=value)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar

  # This add the bars with a blue color
  geom_bar(stat="identity", fill=alpha("blue", 0.3)) +

  # Limits of the plot = very important. The negative value controls the size of the inner circle, the positive one is useful to add size over each bar
  ylim(-100,120) +

  # Custom the theme: no axis title and no cartesian grid
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-2,4), "cm")     # This remove unnecessary margin around plot
  ) +

  # This makes the coordinate polar instead of cartesian.
  coord_polar(start = 0)

and looks like: Original

When I change the number of entries to 360, at least this is working:

data=data.frame(
  id=seq(0,360),
  individual=paste( "Mister ", seq(0,360), sep=""),
  value=sample( seq(0,360), 361, replace=T)
)

Number changed

Now I want to connect these 361 entries with values from my own:

APD_first= c(5244, 5171, 5124, 5160, 5169, 5255, 5034, 5324, 5277, 5217, 5268, 5295, 5326, 5484, 5324, 5449, 5418, 5531, 5715, 5609, 5484, 5678, 5635, 5457, 5611, 5737, 5601, 5760, 5582, 5608, 5846, 5786, 5784, 5957, 5898, 5844, 5828, 5882, 5980, 6002, 6056, 5922, 5979, 6042, 5960, 5990, 6009, 5950, 6075, 5965, 5973, 5838, 5949, 5868, 5726, 5748, 5617, 5671, 5685, 5621, 5454, 5701, 5606, 5546, 5550, 5466, 5474, 5426, 5343, 5395, 5395, 5343, 5327, 5320, 5227, 5247, 5276, 5254, 5373, 5310, 5245, 5082, 4970, 5033, 4980, 5033, 5001, 4928, 5168, 4972, 5113, 5018, 5128, 5109, 5108, 5096, 5082, 5094, 5073, 5099, 4966, 4999, 4946, 5046, 5030, 4957, 4924, 4970, 4937, 4998, 4939, 4959, 4951, 4953, 4940, 5093, 4961, 4935, 4990, 4998, 4938, 4958, 4974, 4990, 5001, 5013, 5077, 4968, 4896, 4960, 5024, 4964, 4852, 5069, 4959, 5047, 4999, 4957, 5145, 5095, 5056, 5212, 5153, 5139, 5125, 4981, 5116, 4994, 4997, 5059, 5104, 5133, 4954, 5054, 5132, 5071, 5049, 5057, 5367, 5143, 5150, 5207, 5149, 5270, 5181, 5251 ,5160, 5180, 5231, 5277, 5227, 5259, 5275, 5127, 5200, 5206, 5252, 5250, 5211, 5343, 5330, 5438, 5402, 5389, 5426, 5534, 5431, 5592, 5650, 5630, 5680, 5669, 5664, 5696, 5643, 5862, 5674, 5923, 5721, 5804, 5890, 5925, 6021, 6075, 6075, 6058, 6128, 6723, 6244, 6099, 6202, 6086, 6240, 6245, 6122, 6257, 6102, 6068, 6176, 6311, 6311, 6154, 6331, 6212, 6238, 6231, 6237, 6181, 6187, 6176, 6167, 6119, 6319, 6321, 6298, 6318, 6380, 6306, 6330, 6413, 6330, 6435, 6377, 6419, 6503, 6561, 6426, 6376, 6311, 6461, 6440, 6466, 6393, 6435, 6503, 6403, 6390, 6361, 6444, 6319, 6278, 6235, 6313, 6238, 6282, 6344, 6255, 6311, 6208, 6236, 6194, 6284, 6287, 6362, 6281, 6332, 6237, 6345, 6203, 6422, 6409, 6365, 6383, 6439, 6440, 6383, 6298, 6374, 6450, 6356, 6450, 6343, 6326, 6357, 6427, 6470, 6366, 6296, 6160, 6408, 6227, 6173, 6156, 6261, 6043, 6282, 6209, 6136, 6147, 6106, 6113, 5998, 6006, 6002, 5894, 5877, 5847, 5825, 5784, 5825, 5840, 5819, 5826, 5806, 5837, 5715, 5749, 5765, 5553, 5598, 5614, 5519, 5547, 5658, 5635, 5577, 5454, 5410, 5524, 5432, 5388, 5342, 5390, 5563, 5281, 5240, 5283, 5257, 5158, 5097, 5220, 5148, 5192, 5133, 5203, 5176, 5238, 5123, 5210, 5126, 5234)

with following code modifications:

data=data.frame(
  id=seq(0,360),
  individual=paste( "Mister ", seq(0,360), sep=""),
  # value=sample( seq(0,360), 361, replace=T)
  value = APD_first
)

p = ggplot(data, aes(x=as.factor(id), y=value)) +      
    geom_bar(stat="identity", fill=alpha("blue", 0.3)) +
    ylim(0,7000) +

    theme_minimal() +
    theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-2,4), "cm") 
  ) +
  coord_polar(start = 0)
p

But this looks like:

Own values

What am I doing wrong?

edit: I changed the code to:

# Create dataset
data=data.frame(
  id=seq(0,360),
  Angle=paste( "", seq(0,360), sep=""),
  # value=sample( seq(90,100), 361, replace=T)
   value = APD_first
)

# Make the plot
p = ggplot(data, aes(x=as.factor(id), y=value)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar

  # This add the bars with a blue color
  geom_bar(stat="identity", fill=alpha("blue", 0.3)) +

  # Limits of the plot = very important. The negative value controls the size of the inner circle, the positive one is useful to add size over each bar
  ylim(-4000,6000) +

  # Custom the theme: no axis title and no cartesian grid
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-2,4), "cm")     # This remove unnecessary margin around plot
  ) +

  # This makes the coordinate polar instead of cartesian.
  coord_polar(start = 0)
p

New

It seems I'm getting closer but I can't figure out the issue now.. Goal is to represent a circle which consists of 360 degrees. Background is that I've rotated a device in 360 degrees and measured a parameter at each degree. Now I want to visualize this. That means I have a circular bar plot of 360 bars and each with a certain value between 4000 and 7000.

editedit: Ok.. I've mixed up the upper limit which should be 7000 instead of 6000 (as written above in the code..). That's why I ignored some bars:

Final

0 Answers
Related