如何更改ggplot2图例以包含负值和正值?

时间:2018-05-08 22:10:26

标签: r ggplot2 legend

我正试图创造一个消极和积极的传奇,而不是我所拥有的。以下是我可重现的代码:

library(ggplot2)

YEAR <- c(1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969)
ATL <- c(0.9814819, 0.6158561, 0.1594867, -1.324053, -0.5223706,
         0.4015643, -1.255614, 0.6481795, 0.1342264, -0.507981)
OBS <- c(-0.2040433, -0.2014844, -0.39053, 0.5320961, -0.870454,
         0.7047542, 1.069224, -0.217602, 0.3252735, 1.419028)
GLOB_OBS_SVD <- c(-0.1309537, -1.074651, 2.5286819, 1.4785519, 0.061268,
                  -2.547872, -1.171241, -0.3161392, 0.4983722, -0.7510358)

PC1 <- data.frame(YEAR, ATL, OBS, GLOB_OBS_SVD)

PC1$GLOB_OBS_SVD <- PC1$GLOB_OBS_SVD*-1.0

PC1$DOT_SIZE <- ifelse(PC1$GLOB_OBS_SVD < 0.5 & PC1$GLOB_OBS_SVD > -0.5, 0, 
                       ifelse(PC1$GLOB_OBS_SVD < 1.5 & PC1$GLOB_OBS_SVD > -1.5, 1,
                              ifelse(PC1$GLOB_OBS_SVD < 2.5 & PC1$GLOB_OBS_SVD > -2.5, 2, 3
                              )))

PC1$COLOR <- ifelse(PC1$GLOB_OBS_SVD < 0, "deepskyblue", "tomato")

ggplot(PC1, aes(x=ATL, y=OBS)) +
  geom_point(aes(size=DOT_SIZE, color=COLOR)) + 
  scale_size(name = "SVD TS Value", 
             labels = c("-0.5<x<0.5", "0.5<x<1.5 & -0.5<x<-1.5", 
                        "1.5<x<2.5 & -1.5<x<-2.5", "x<-2.5 & x>2.5")) +
  scale_color_manual(values = c("deepskyblue", "tomato"), 
                     labels = c("Negative", "Positive")) +
  labs(color = "SVD TS Value") +
  labs(x = "CESM ATL Nino 4 Index", y = "OBS Nino 4 Index") +
  geom_text(aes(label=YEAR), hjust=0.5, vjust = 0, size = 2.5, angle = 0) +
  theme(plot.title = element_text(hjust = 0.5))

产生这个情节

Scatterplot

但我希望我的传奇看起来像这样,而不是我的混乱:

Legend I want

1 个答案:

答案 0 :(得分:1)

只要您在图例键的一侧而不是两者上都有标签就行了(我觉得这很奇怪),这可以通过现有的图例代码实现。您只需手动定义所需的中断和标签。

PROCESS RESPONSE 
Warning: file_get_contents(apiUrlHere): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
 in /home/ubuntu/workspace/process.php on line 63

Call Stack:
    0.0009     242968   1. {main}() /home/ubuntu/workspace/process.php:0
    0.3491     256144   2. file_get_contents() /home/ubuntu/workspace/process.php:63

enter image description here