简体   繁体   中英

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.

在此处输入图片说明

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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM