自定义GGR绘图图例
我想手动调整我用ggplot制作的绘图中的图例,并向图例中添加两个新项目。现在图例显示为图例标题因子(CruiseID),后面是一个红色圆圈,带有文本201905,然后是一个蓝色圆圈,带有文本201906,尽管代码中有比例填充手动代码。自定义GGR绘图图例,r,ggplot2,legend,R,Ggplot2,Legend,我想手动调整我用ggplot制作的绘图中的图例,并向图例中添加两个新项目。现在图例显示为图例标题因子(CruiseID),后面是一个红色圆圈,带有文本201905,然后是一个蓝色圆圈,带有文本201906,尽管代码中有比例填充手动代码。 我希望没有传奇标题,后面是 红色圆圈和文字2019 MAB支腿1, 蓝色圆圈和文字2019 MAB支腿2, 灰盒与文本调查领域 带有黑色轮廓和文本访问区域的白色框 示例数据 data<-data.frame(CruiseID=c(rep(201905,5)
我希望没有传奇标题,后面是 红色圆圈和文字2019 MAB支腿1, 蓝色圆圈和文字2019 MAB支腿2, 灰盒与文本调查领域 带有黑色轮廓和文本访问区域的白色框 示例数据
data<-data.frame(CruiseID=c(rep(201905,5),rep(201906,5)),
beglat=c(36.66,36.66,37.07,37.01,37.03,37.033,37.08,37.09,37.07,37.077),
beglong=c(-74.75,-74.75,-74.73,
-74.731,-74.90,-74.90,-74.88,-74.88,
-74.72,-74.72))
ggplot() +
geom_point(data = data, aes(x = beglong, y = beglat,colour=factor(CruiseID))) +
scale_colour_manual(values = c("red", "blue"),drop=T)+
xlim(-76,-71)+
ylim(36,42)+
ggtitle("2019 MAB Survey Stations") +
labs(x = "Longitude", y = "Latitude") +
scale_fill_manual(values = c("red", "blue","gray87","black"),
labels = c('Leg 1', 'MAB Leg 2','Survey Domain','Access Area'))+
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title=element_text(size=14),
text=element_text(size=12),
axis.text.x=element_text(colour="black", size = 12),
axis.text.y=element_text(colour="black", size = 12),
legend.justification=c(.5,.5),
legend.background = element_rect(fill = "white", color = NA),
legend.position=c(0.8, 0.3),legend.box="vertical",
legend.margin=margin())
data这里有一个解决方案,它使用ggnewscale::new_scale_color
为颜色创建两个比例,一个用于点,另一个用于框。此外,我还创建了一个名为df的附加data.frame,用于使用geom_rect
绘制2个矩形,并为框创建图例
library(ggplot2)
library(ggnewscale)
# Create another data frame to plot the rectangles and its legend
df <- data.frame (xmin = c(0,0),
xmax = c(0,0),
ymin = c(0,0),
ymax = c(0,0),
fill = c("A","B"))
ggplot() +
geom_point(data = data, aes(x = beglong,
y = beglat,
colour=factor(CruiseID))) +
# Move the color scale for the points before setting a new scale color
scale_color_manual(values = c("red", "blue"),
labels = c('2019 MAB Leg 1', '2019 MAB Leg 2'))+
# Set new scale color
new_scale_color() +
# plot rectangles in 0,0; where they will not appear in the plot's area
geom_rect(data = df,aes(xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax,
fill = fill,
col = fill)) +
# Manually set color for the boxes
scale_color_manual(values = c("white","black"),
labels = c('Survey Domain', 'Access Area')) +
# Manually set fill for the boxes, use same labels as color so the box legends are combined
scale_fill_manual(values = c("gray87","white"),
labels = c('Survey Domain', 'Access Area'))+
xlim(-76,-71)+
ylim(36,42)+
ggtitle("2019 MAB Survey Stations") +
labs(x = "Longitude", y = "Latitude") +
theme_bw()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title=element_text(size=14),
text=element_text(size=12),
axis.text.x=element_text(colour="black", size = 12),
axis.text.y=element_text(colour="black", size = 12),
legend.justification=c(.5,.5),
# Remove legend title
legend.title = element_blank(),
legend.background = element_rect(fill = "white", color = NA),
legend.position=c(0.8, 0.3),legend.box="vertical",
legend.margin=margin())
库(ggplot2)
图书馆(ggnewscale)
#创建另一个数据框以绘制矩形及其图例
df在前面,@RuiBarradas关于scale\u fill\u manual
的评论是正确的,并且从您的代码中被误导了。我相信您将其包括在内,是希望稍后添加您的区域(调查和访问)。也许你认为这会对你的色彩传奇产生影响(或者只是把他们弄糊涂了)。不管怎样,他的观点是正确的:scale\u fill\u manual
这里什么都不做,除非你在绘图的某处实际使用fill=
作为aes
thetic。所有的命名都必须在scale\u color\u手册
中完成
此外,您有两个设置标签的选项。我相信您可以在scale调用中使用labels=
来设置它们,这可以是c(“201905”=“MAB Leg 1”,…)
的命名向量。或者您可以在美学中最初使用标签(如下所示)。它们都可以工作,但前者需要更多的数据管理,而不需要ggplot
我建议使用稍微不同的数据组织
- 由于您不想显示
201905
(etc)编号,但您想在其上指定标签和颜色,因此我将创建一个标签
框架,将它们映射在一起,然后我将合并它们
- 由于您有
scale\u fill\u manual
但未指定任何fill=
美学,因此我将创建一个包含两种类型区域的伪区域
框架。我将为它们指定任意的CruiseID
值,这样我们仍然可以使用标签
框架来识别它们(以及它们的填充)
标签+scale\u fill\u手册(name=NULL,…)
?(为了删除传奇标题)我已经尝试过了,但没有任何效果。似乎整个比例填充手动代码部分都无法识别scale\u fill\u manual
如果没有美观的映射fill
到变量,则无法识别。在aes(.)
中,您有颜色
,而不是填充
。