0

I'm struggling to get polar_coords to work as I had hoped. I want each item to be represented by a coloured track, with a range of 1:50000. I then wanted to plot points over these tracks at the corresponding locations, with symbols representing the different categories. The points would then be annotated with the id.

Dataframe:

structure(list(item = structure(c(1L, 2L, 2L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L), .Label = c("AA", "AB", "AC", "AD", "AE", 
"BA", "BB", "BC", "BD", "BE"), class = "factor"), location = c(10045L, 
12041L, 15035L, 22054L, 19023L, 49411L, 39012L, 3041L, 23065L, 
33015L, 42069L, 26859L), category = structure(c(1L, 1L, 2L, 3L, 
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L), .Label = c("X", "Y", "Z"), class = "factor"), 
    id = structure(c(1L, 8L, 2L, 7L, 6L, 10L, 5L, 1L, 1L, 3L, 
    4L, 9L), .Label = c("Apple", "Banana", "Cherry", "Grape", 
    "Mango", "Melon", "Orange", "Pear", "Raspberry", "Strawberry"
    ), class = "factor")), .Names = c("item", "location", "category", 
"id"), class = "data.frame", row.names = c(NA, -12L))

2 Answers2

2

Maybe you can work from this:

ggplot(data,aes(x=location, color=id, y=id)) + 
geom_linerange(aes(y=id, xmin=0, xmax=50000, color=category), size=2, alpha=0.5) + 
geom_point(size=3) + 
coord_polar()

enter image description here

ziggystar
  • 28,410
  • 9
  • 72
  • 124
  • Thanks, this definitely looks workable! I get the following error when running my side: (`Warning: Ignoring unknown aesthetics: y, xmin, xmax Error: geom_linerange requires the following missing aesthetics: ymin, ymax`) – Michael Crichton Mar 03 '21 at 09:37
2
my_data %>%
  ggplot(aes(item, location, shape = category, label = id)) + 
  geom_col(aes(y = Inf), fill = "gray80") +
  geom_point(size = 3)  +
  geom_text(vjust = -1) +
  scale_x_discrete(expand = expand_scale(add = c(5,0))) +
  coord_polar(theta = "y") +
  theme_void()

enter image description here

If you want a break in the middle, you could change the item to a numeric value relating to it's desired position:

my_data %>%
  mutate(item_pos = as.numeric(item),
         item_pos = item_pos + if_else(item_pos > 5, 1, 0)) %>%
  ggplot(aes(item_pos, location, shape = category, label = id)) + 
  ...

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thanks, this is really helpful, and expanding the x axis is a really neat way of creating a void in the middle! I was hacking through adding blank items to get the same end goal. I've worked with your example and got it 99% of the way there - the only thing I can't figure out is how to create some white space between the two groups (AX, BX). I expanded y lim to 50k, and added a manual fill scale to make AA-E red, and BA-E blue. I'd like to get a little space between the red and blue, but can't seem to resolve that with the current discrete X without overwriting it. – Michael Crichton Mar 03 '21 at 10:24
  • For now I've used my previous hacky way of adding a factor and leaving it white. Thanks again so much for your help! – Michael Crichton Mar 03 '21 at 12:41
  • @MichaelCrichton You could also use [this answer](https://stackoverflow.com/questions/18202946/formatting-positions-on-discrete-scale-in-ggplot2) to specify the exact y-positions for all values. But it's probably more work than your hack. – ziggystar Mar 03 '21 at 14:07