【问题标题】:How to specify shape for legend to match the groups' points in scatterplot using R?如何使用 R 指定图例的形状以匹配散点图中的组点?
【发布时间】:2018-09-26 11:13:35
【问题描述】:

这是我的数据:

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"))

我需要将图例的形状与分组散点图中的点匹配,您能帮帮我吗? 谢谢

【问题讨论】:

  • 此代码不运行,例如geom_errorbar(aes(ymin= nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+ = nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+ 中间少了几个字。您能否更新一下,并澄清您的问题?
  • @Sally,建议使用dput显示数据。请参阅here 了解如何使用它。简而言之,将您的数据框包装在 dput 中。例如dput(iris),按回车键。然后在此处复制粘贴输出结构。人们更容易为您提供帮助。

标签: r ggplot2


【解决方案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"))

【讨论】:

  • 非常感谢您在编辑我的代码时提供的大力帮助。
  • 当我运行它时它给出了这个错误:错误:无法将 ggproto 对象添加在一起。您是否忘记将此对象添加到 ggplot 对象中?
  • 我有一个错字,在 geom_point 行的末尾有一个逗号而不是一个 +。很抱歉!
  • 谢谢!我确实把它改成了逗号,但仍然出现错误,你猜吗?
  • 是的,我遇到了同样的错误:错误:无法将 ggproto 对象添加在一起。您是否忘记将此对象添加到 ggplot 对象中?
猜你喜欢
  • 2011-10-03
  • 2019-05-18
  • 2022-06-11
  • 2021-11-25
  • 1970-01-01
  • 2015-07-12
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多