Search code examples
rggplot2color-schemepolar-coordinatesspiral

Spiral (Polar) plot based on conditional color in R?


I want all negative value to be yellow and all positive to be blue. Something is wrong with my code as I can see some of the positive values are turned yellow and vice versa for blue (see the figure). Also the legend scale did not show up at the bottom. Also, how would I make the zero line bold. Here is the sample of my code

A=runif(20,min = -3, max = 3)
myDate=1981:2000
myData=data.frame(myDate,A)

ggplot(myData, aes(myDate, A))+
  geom_bar(stat = "identity", fill=ifelse( A < 0,"yellow","blue"))+
  coord_polar(theta = "x")+
  theme(legend.position = "bottom")+
  theme_bw()

Any help would be appreciated.

enter image description here


Solution

  • The issues are not related to the polar coordinates.
    For the color you have to put the conditional formatting into an aes() call to work. For the legend you have to put the alteration to the theme behind the theme_bw() call. Otherwise it is overwritten again. You can just plot a line at 0 to make the line thicker.
    Hope this helps.

    A=runif(20,min = -3, max = 3)
    myDate=1981:2000
    myData=data.frame(myDate,A)
    
    ggplot(myData, aes(myDate, A))+
      geom_bar(stat = "identity", aes(fill=ifelse( A < 0,"negative","positive")))+
      coord_polar(theta = "x")+
      scale_fill_manual(values = c("yellow", "blue")) +
      geom_hline(yintercept = 0, color = "black")+
      labs(fill = "Legend") +
      theme_bw() +
      theme(legend.position = "bottom")
    

    enter image description here