gpt4 book ai didi

r - 如何在 ggplot 中为一种美学创建多个图例?

转载 作者:行者123 更新时间:2023-12-02 16:14:48 25 4
gpt4 key购买 nike

我正在尝试做与此图的面板 B 中类似的事情:https://ars.els-cdn.com/content/image/1-s2.0-S0002929721000938-gr3_lrg.jpg ,其中我使用形状作为图例,并使用闭合或开放形状作为第二个图例。

我使用以下示例数据

example_data <- data.frame(x=c(1,3,2,4,5,3,1,2,3,2),
y=c(3,5,7,9,1,3,4,7,8,9),
color=c('col1','col2','col3','col1','col2','col3','col1','col2','col1','col3'),
shape=c('triangle','circle','triangle','circle','triangle','circle','triangle','circle','triangle','circle'),
openclosed=c('open','open','open','open','open','closed','closed','closed','closed','closed'))

我设法用不同的形状绘制一个图,并根据列打开/关闭

example_data$point_shape <- -1
example_data[example_data$shape=='triangle' & example_data$openclosed=='closed',]$point_shape <- 15
example_data[example_data$shape=='triangle' & example_data$openclosed=='open',]$point_shape <- 0
example_data[example_data$shape=='circle' & example_data$openclosed=='closed',]$point_shape <- 16
example_data[example_data$shape=='circle' & example_data$openclosed=='open',]$point_shape <- 1
example_data$point_shape <- as.character(example_data$point_shape)


ggplot(example_data, aes(x, y, colour=color, shape=point_shape))+
geom_point()+
scale_shape_manual(name='', breaks=unique(example_data$point_shape),
values=as.numeric(unique(example_data$point_shape)),
labels=c('Open square','Open circle',
'Closed circle','Closed square'))

example

然后我尝试了 https://stackoverflow.com/a/44725969/651779得到两个形状图例

ggplot(example_data, aes(x, y, colour=color, shape=point_shape))+
geom_point(show.legend=F)+
geom_point(data = example_data[example_data$shape %in% c('triangle','circle'),],
size=0,alpha=0)+
geom_point(data = example_data[example_data$openclosed %in% c('open','closed'),],
size=0, alpha=0)+
scale_shape_manual(name='', breaks=unique(example_data$point_shape),
values=as.numeric(unique(example_data$point_shape)),
labels=c('Open square','Open circle',
'Closed circle','Closed square'))+
guides(alpha = guide_legend(title = "Shape", order = 1,
override.aes = list(shape = c(15,16),
size = 5, color = "black",alpha=1)),
size = guide_legend(title = "Open or closed", order = 2,
override.aes = list(shape = c(16,1),
size = 5, color = "black", alpha=1)))

example 2

但这并没有添加两个单独的图例。

如何将图例更改为具有 1 个方形/圆形图例和另一个开放/闭合形状的图例?

最佳答案

为此,我个人喜欢 ggnewscale 包。不确定您想要如何安排图例 - 我使用不同的形状而不是更改填充。

library(tidyverse)
library(ggnewscale)
example_data <- data.frame(x=c(1,3,2,4,5,3,1,2,3,2),
y=c(3,5,7,9,1,3,4,7,8,9),
color=c('col1','col2','col3','col1','col2','col3','col1','col2','col1','col3'),
shape=c('triangle','circle','triangle','circle','triangle','circle','triangle','circle','triangle','circle'),
openclosed=c('open','open','open','open','open','closed','closed','closed','closed','closed'))


ggplot(mapping = aes(x, y))+
geom_point(data = filter(example_data, shape == "circle"),
aes(shape = openclosed)) +
# it's important to give a different name to this scale than to the second scale
# This can also be NULL
scale_shape_manual("Circle", values = c(open = 1, closed = 16)) +
new_scale("shape") +
geom_point(data = filter(example_data, shape == "triangle"),
aes(shape = openclosed)) +
scale_shape_manual("Triangle", values = c(open = 2, closed = 17))

reprex package 创建于 2021-04-11 (v1.0.0)

关于r - 如何在 ggplot 中为一种美学创建多个图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67044054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com