基于R?中条件颜色的螺旋(极点)图

时间:2019-05-02 14:36:36

标签: r ggplot2 color-scheme polar-coordinates spiral

我希望所有负值都为黄色,所有正值都为蓝色。我的代码出了点问题,因为我看到一些正值变成黄色,反之亦然变成蓝色(见图)。图例比例也没有显示在底部。另外,我如何将零线加粗。这是我的代码示例

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

任何帮助将不胜感激。

enter image description here

1 个答案:

答案 0 :(得分:1)

问题与极坐标无关。
对于颜色,您必须将条件格式放入aes()调用中才能起作用。对于图例,您必须在主题theme_bw()之后加上对主题的更改。否则,它将再次被覆盖。 您可以在0处画一条线以使其更粗。
希望这可以帮助。

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

相关问题