这是我的数据:
Food nutrient1 nutrient2 nutrient1se nutrient2se
Control 50 1 2.5 0.02
D1 100 1 4 0.05
D2 90 0.9 3 0.03
D3 100 0.9 6 0.04
我的代码是:
library(ggplot2)
ggplot (data = d, aes(x= nutrient2, y = nutrient1, group=Food))+
geom_point (aes (shape=Food, colour=Food), size=4, shape=c(15,16,17,18))+
geom_errorbarh(aes(xmin=nutrient2-nutrient2se, xmax=nutrient2+nutrient2se), length=0.2, colour="orange")+
geom_errorbar(aes(ymin= nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+
= nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+
scale_size_area()+
xlim(0, 1.5)+
ylim(90, 120)+
xlab("Total nutrient2 (g)") +
ylab("Total nutrient1 (g)") +
theme_update(plot.title = element_text(hjust = 0.5))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "orange"))+
guides(color=guide_legend(override.aes=list(fill=NA)))+
plot.background=element_rect(fill="transparent",colour=NA),
legend.key = element_rect(fill = "transparent", colour = "transparent"))+
theme(axis.title.x = element_text(colour = "orange"),axis.title.y = element_text(colour = "orange"))
我需要将图例的形状与分组散点图中的点进行匹配,您能帮我吗? 谢谢
答案 0 :(得分:1)
(请注意,原始代码有错别字。请尝试提供有效的代码,以使人们更容易获得帮助。)
问题似乎出在,您同时为geom_points设置和映射了形状。
https://www.superdatascience.com/ggplot2-setting-vs-mapping-aesthetics/
使用aes()
函数“映射”时,ggplot将添加图例。 “设置”时,您是在手动设置图表元素的外观,并且ggplot不会添加图例。如果要“映射”美观,但要覆盖默认值并为如何映射值进行特定选择,则可以使用aes()
,然后使用scale_manual_*
来指定您的选择。
https://ggplot2.tidyverse.org/reference/scale_manual.html
以下是您对代码的一些修改,可能与您要查找的内容更接近。
d <- read.table(header = T, text = "
Food nutrient1 nutrient2 nutrient1se nutrient2se
Control 50 1 2.5 0.02
D1 100 1 4 0.05
D2 90 0.9 3 0.03
D3 100 0.9 6 0.04")
library(ggplot2)
ggplot (data = d, aes(x= nutrient2, y = nutrient1, group=Food))+
geom_point (aes (shape=Food, colour=Food), size=4) +
# Note, above line originally ended "shape=c(15,16,17,18))+"
geom_errorbarh(aes(xmin=nutrient2-nutrient2se,
xmax=nutrient2+nutrient2se),
# length=0.2,
colour="orange")+
geom_errorbar(aes(ymin= nutrient1-nutrient1se,
ymax= nutrient1 +nutrient1se),
width=0.2, colour="orange")+
# The code below is missing a geom_*
# = nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+
scale_size_area()+
scale_shape_manual(values = c(15,16,17,18)) +
xlim(0, 1.5)+
ylim(90, 120)+
xlab("Total nutrient2 (g)") +
ylab("Total nutrient1 (g)") +
# Reordered this out of the theme() function
guides(color=guide_legend(override.aes=list(fill=NA)))+
theme_update(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "orange"),
plot.background=element_rect(fill="transparent",colour=NA),
legend.key = element_rect(fill = "transparent",
colour = "transparent"))+
theme(axis.title.x = element_text(colour = "orange"),
axis.title.y = element_text(colour = "orange"))