更改 geom_point 散点图中第三个变量向量的名称;图表2

Changing the name of a 3rd variable vector in a geom_point scatterplot; ggplot2

我正在使用 R 中的 ggplot2 创建一个散点图,其中图中点的大小和颜色对应于第三个变量的平均值。我已经想出如何让情节看起来像我想要的那样,但我无法弄清楚如何更改图例中出现的变量的名称(更改为更具描述性和空格的名称)。我发现了类似的更改 facet labels: and for changing the title of a traditional legend in the ggplot2 documentation using scale_color_discrete 的帖子,但这些选项似乎不起作用,因为我正在定义该图例以不同方式表示的第三个变量。这是一个例子:

生成数据:

x<-rnorm(150)
y<-rnorm(150)
d<-cbind.data.frame(x, y)
d$OUT_VAR<-2 + 1.2*d$x + 1.2*d$y

创建地块:

plot1<-ggplot(d, aes(x, y)) +
  geom_point(aes(size = OUT_VAR, alpha = OUT_VAR)) +
  scale_size_continuous(breaks=c(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6)) +
  scale_alpha_continuous(breaks=c(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6)) +
  xlim(-4, 4) +
  ylim(-4, 4) +
  geom_segment(x = -4, xend = 4, y = 0, yend = 0) +
  geom_segment(x = 0, xend = 0, y = -4, yend = 4) +
  theme_classic() + 
  ggtitle("Plotting the value of the outcome
          on the X, Y Plane") +
  xlab("X Variable") +
  ylab("Y Variable") + 
  theme(title = element_text(size = 14)) + 
  theme(legend.title = element_text(size = 9)) +
  theme(axis.title.x = element_text(size = 12)) +
  theme(axis.title.y = element_text(size = 12)) 

plot1

这将创建以下图:

Sample Plot

而不是 'OUT_VAR',我想重命名图例标题...可能类似于 'Outcome Variable'。如果一个词可行,我意识到我可以重命名矢量本身,但这是针对专业出版物的,所以我希望能够添加更具描述性的内容。非常感谢任何建议!

scale_size_continuous(breaks=c(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6),
    name = "Outcome Variable") +
  scale_alpha_continuous(breaks=c(-3, -2, -1, 0, 1, 2, 3, 4, 5, 6),
    name = "Outcome Variable")