Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用ggplot()时编写手动图例_R_Ggplot2 - Fatal编程技术网

在R中使用ggplot()时编写手动图例

在R中使用ggplot()时编写手动图例,r,ggplot2,R,Ggplot2,我有下面的图p: 为此,我希望图例与其他情节类似: 因为p是两个不同曲线图p1和p2的组合,所以我使用了: p<- grid.arrange(p1, p2, ncol = 3, widths = c(3,5,0)) 请告诉我是否有更好的解决方案。感谢您的帮助 如果您希望添加包含所有3个ID的图例,这是一种方法。您可以像之前一样创建绘图,然后只需使用下面的函数g_legend,即可获取具有3个ID的图例。然后从绘图中删除图例,并将3个对象(2个绘图和图例)放入网格。排列。我指定了一个布局矩

我有下面的图
p

为此,我希望图例与其他情节类似:

因为
p
是两个不同曲线图
p1
p2
的组合,所以我使用了:

p<- grid.arrange(p1, p2, ncol = 3, widths = c(3,5,0))

请告诉我是否有更好的解决方案。感谢您的帮助

如果您希望添加包含所有3个ID的图例,这是一种方法。您可以像之前一样创建绘图,然后只需使用下面的函数
g_legend
,即可获取具有3个ID的图例。然后从绘图中删除图例,并将3个对象(2个绘图和图例)放入
网格。排列
。我指定了一个布局矩阵,以显示您可以根据每个对象占用的空间进一步自定义它

df1 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Time,~Collar,
                       0.242, 0.173, 0.329, "Day","41361´",
                       0.216, 0.152, 0.296, "Night","41361´")


df2 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Time,~Collar,
                       0.290, 0.214, 0.381, "Day","41366´",
                       0.256, 0.186, 0.342, "Night","41366´")


df<-rbind(df1,df2)

dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper,
         linegroup = paste(Time, Collar))



p1<-ggplot(data = dfnew, aes(x = Time, y = Proportion, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1) +
  scale_shape_manual(values=c("41361´"=19,"41366´"=15)) +
  scale_color_manual(values = c("Day" = "black", 
                                "Night" = "black")) + 
  labs(shape="Collar ID") + ylim(0.05, 0.4) #+ theme(legend.position = "none")



df1 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Collar,
                       0.181, 0.148, 0.219, "LGCA","41361´",
                       0.289, 0.242 ,0.341 , "SNP","41361´")

df2 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Collar,
                       0.099, 0.096, 0.104, "LGCA","41365´",
                       0.224, 0.217 ,0.232 , "SNP","41365´")

df<-rbind(df1,df2)



dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper,
         linegroup = paste(Area, Collar))




p2<-ggplot(data = dfnew, aes(x = Area, y = Proportion, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1) + scale_shape_manual(values=c("41361´"=19,"41365´"=17)) + scale_size_manual(values=c(2,2)) +
  scale_color_manual(values = c("SNP" = "black", 
                                "LGCA" = "black")) + labs(shape="Collar ID") + ylim(0.05, 0.4) +
  theme(legend.text=element_text(size=18))+
  theme(legend.title = element_text(size=18))

#+ theme(legend.position = "none")


g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}
#put in plot with 3 ids in the g_legend function
aleg <- g_legend(p1)

p1 <- p1+ theme(legend.position = "none")
p2 <- p2+ theme(legend.position = "none")



lay <- rbind(c(1,1,2,2,3),
             c(1,1,2,2,3))


gridExtra::grid.arrange(p1, p2,
                        #use layout matrix to set sizes
                        layout_matrix=lay,
                        # add legend
                        aleg)

df1这里有两种方法可以避免所有的
ggproto
东西。我正在简化一些数据创建,按时间创建一个数据帧
,按区域创建一个
,这样您就可以同时使用它们了。我添加了一个步骤,使
Collar
成为每个数据帧的一个因子,这将用于第二种方法

库(dplyr)
图书馆(GG2)
图书馆(tidyr)
按时间百分比
突变(ymin=比例-较低,
ymax=比例+上限,
线组=粘贴(时间、项圈),
轴环=总系数(轴环))
按面积%
突变(ymin=比例-较低,
ymax=比例+上限,
线组=粘贴(面积、项圈),
轴环=总系数(轴环))
第一种方法是将数据帧一起操纵成一个形状,在这个形状中您可以只使用面。将每个数据帧制作成一个长形,其中标记每个数据帧的关键点是时间或面积,用于刻面。我把时间切换到第一级,这样看起来更像你的

######带刻面
df_长%聚集(键、值、时间),
按面积%>%聚集(键、值、面积)
) %>%
变异(key=forcats::fct_relevel(as.factor(key),“Time”))
头部(df_long)
#>#tibble:6 x 9
#>ymax线条组键值中上下领的比例
#>                         
#>1 0.242 0.173 0.329 41361'0.069 0.571天41361'时间天
#>2 0.216 0.152 0.296 41361'0.064 0.512夜41361'时间夜
#>3 0.290 0.214 0.381 41366'0.0760 0.671天41366'时间天
#>4 0.256 0.186 0.342 41366´0.07 0.598夜41366´时间夜
#>5 0.181 0.148 0.219 41361'0.033 0.4 LGCA 41361'区域LGCA
#>6 0.289 0.242 0.341 41361'0.0470 0.63单核苷酸多态性41361'区域单核苷酸多态性
然后添加一些选项,使刻面打印看起来更像您想要的,将刻面条放在底部,使其看起来像轴标题

ggplot(df_long,aes(x=值,y=比例,组=线组))+
几何点(aes(形状=衣领),位置=位置减淡(宽度=0.4))+
几何误差条(aes(ymin=下,ymax=上),位置=位置减淡(宽度=0.4),宽度=0.2)+
小平面包裹(变量(键),刻度=“自由”,strip.position=“底部”)+
实验室(x=NULL)+
主题(strip.placement=“外部”,
strip.background=元素_blank())

如果出于任何原因,这对您都不起作用,第二种方法是制作两个绘图,并使用
cowplot::plot\u grid
对它们进行排列。您还可以使用其他一些软件包来代替(
patchwork
是另一个我喜欢的软件包)。这里的诀窍是为一个包含所有因子水平的绘图创建图例;我将使用
forcats::fct_expand
来实现这一点,以将一个数据帧的级别添加到另一个数据帧。由于面积图将在右侧,因此我正在调整因子级别并制作图例。在比例中设置
drop=F
,以便图例显示所有级别,即使这些级别不在数据中

######带绘图网格
p_时间%
ggplot(aes(x=面积,y=比例,组=线组))+
几何点(aes(形状=衣领),位置=位置减淡(宽度=0.4))+
几何误差条(aes(ymin=下,ymax=上),位置=位置减淡(宽度=0.4),宽度=0.2)+
连续刻度(限值=c(0,0.6))+
比例\形状\离散(下降=F)
我还设置了y轴限制,以匹配两个图,并根据需要进行调整

然后提取图例,并将所有内容放在
cowplot::plot_grid
中,从各个绘图中删除图例。这样做的原因是,您可以使两个绘图具有相同的大小,而不必为图例留出空间


legend+1,感谢您在发布前投入大量精力解决此问题,以及深入挖掘
ggplot
内部。不过,我认为这可能比这个简单:您可以做的一件事是确保两个数据子集,或者至少是用于制作图例的数据子集,都具有您想要显示的所有因子级别。然后将
drop=F
添加到您的标尺中,以创建通过一些相关选项运行的图例。如果必要的话,我建议用
forcats
来统一因子水平。还有几个软件包使安排ggplots变得更容易:cowplot、patchwork、ggpubr、egg(我个人没有使用过这个软件包)。这看起来是一个非常好的答案,一定花了相当多的时间。干得好,卡米尔!
df1 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Time,~Collar,
                       0.242, 0.173, 0.329, "Day","41361´",
                       0.216, 0.152, 0.296, "Night","41361´")


df2 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Time,~Collar,
                       0.290, 0.214, 0.381, "Day","41366´",
                       0.256, 0.186, 0.342, "Night","41366´")


df<-rbind(df1,df2)

dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper,
         linegroup = paste(Time, Collar))



p1<-ggplot(data = dfnew, aes(x = Time, y = Proportion, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1) +
  scale_shape_manual(values=c("41361´"=19,"41366´"=15)) +
  scale_color_manual(values = c("Day" = "black", 
                                "Night" = "black")) + 
  labs(shape="Collar ID") + ylim(0.05, 0.4) #+ theme(legend.position = "none")



df1 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Collar,
                       0.181, 0.148, 0.219, "LGCA","41361´",
                       0.289, 0.242 ,0.341 , "SNP","41361´")

df2 <- tibble::tribble(~Proportion, ~Lower,~Upper, ~Area,~Collar,
                       0.099, 0.096, 0.104, "LGCA","41365´",
                       0.224, 0.217 ,0.232 , "SNP","41365´")

df<-rbind(df1,df2)



dfnew <- df %>% 
  mutate(ymin = Proportion - Lower,
         ymax = Proportion + Upper,
         linegroup = paste(Area, Collar))




p2<-ggplot(data = dfnew, aes(x = Area, y = Proportion, group=linegroup)) +
  geom_point(aes(shape = as.character(Collar)), size = 4, stroke = 0)+
  geom_line(aes(group = linegroup),linetype = "dotted",size=1) +
  theme(axis.text=element_text(size=15),
        axis.title=element_text(size=20)) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width=0.3, size=1) + scale_shape_manual(values=c("41361´"=19,"41365´"=17)) + scale_size_manual(values=c(2,2)) +
  scale_color_manual(values = c("SNP" = "black", 
                                "LGCA" = "black")) + labs(shape="Collar ID") + ylim(0.05, 0.4) +
  theme(legend.text=element_text(size=18))+
  theme(legend.title = element_text(size=18))

#+ theme(legend.position = "none")


g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}
#put in plot with 3 ids in the g_legend function
aleg <- g_legend(p1)

p1 <- p1+ theme(legend.position = "none")
p2 <- p2+ theme(legend.position = "none")



lay <- rbind(c(1,1,2,2,3),
             c(1,1,2,2,3))


gridExtra::grid.arrange(p1, p2,
                        #use layout matrix to set sizes
                        layout_matrix=lay,
                        # add legend
                        aleg)