R语言 如何正确设置线型和颜色,同时控制在ggplop中的图例中显示哪个级别

q5iwbnjs  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(104)

我有这个数据集:

data.a <- read.csv(text = "
Obs,treat2,treat1,response,temperature,model
321,A,D,10,100,by_fac
322,A,D,20,1200,by_fac
323,A,E,10,100,by_fac
324,A,E,40,1300,by_fac
257,A,P,20,100,by_fac
258,A,P,60,1200,by_fac
259,B,D,30,100,by_fac
260,B,D,60,1300,by_fac
425,B,E,10,64,by_fac
426,B,E,50,19,by_fac
427,B,P,20,2,by_fac
428,B,P,20,366,by_fac
433,generic_t1,generic_t2,10,98,gen
434,generic_t1,generic_t2,60,256,gen
435,generic_t1,generic_t2,10,78,gen
436,generic_t1,generic_t2,90,271,gen
441,generic_t1,generic_t2,15,48,gen
442,generic_t1,generic_t2,25,8,gen
") %>%
  mutate(treat1 = as.factor(treat1),
         treat2 = as.factor(treat2))

我需要做一个非常接近用这段代码生成的图:

ggplot(data.a, 
       aes(x = temperature, y = response)) +

  geom_line(aes(color= treat1,
                linetype = treat2))+  
  
  scale_color_manual(values = c("red", "green", "black", "orange"),
                     labels = c("D", "E", "generic_t1", "P")) +
  
  scale_linetype_manual(values=c("solid", "dotted", "solid"),
                        labels = c("A", "B", "generic_t2")) +

  theme_bw() +
  facet_wrap(~model,
             ncol = 1)

这就是剧情:

我的问题是我想从图例中删除红色圆圈的级别。基本上,图例对应于上面的面和底部是通用的结果,应该是一个坚实的黑线。
根据一些回复,我尝试从标签中删除级别:

ggplot(data.a, 
       aes(x = temperature, y = response)) +
  
  geom_line(aes(color= treat1,
                linetype = treat2))+  
  
  scale_color_manual(values = c("red", "green", "black", "orange"),
                     labels = c("D", "E", "", "P")) +
  
  scale_linetype_manual(values=c("solid", "dotted", "solid"),
                        labels = c("A", "B", "")) +
  
  theme_bw() +
  facet_wrap(~model,
             ncol = 1)

但我仍然有一些行(用红色标记)我想删除或隐藏。

j0pj023g

j0pj023g1#

我采取了两个步骤来做到这一点。首先,设置处理因子的水平,使通用水平位于最后,并用空格" "标记。然后,我在指南中使用override.aes关闭相关行。我想,这就像你在寻找的东西:

library(ggplot2)
library(dplyr)
data.a <- read.csv(text = "
Obs,treat2,treat1,response,temperature,model
321,A,D,10,100,by_fac
322,A,D,20,1200,by_fac
323,A,E,10,100,by_fac
324,A,E,40,1300,by_fac
257,A,P,20,100,by_fac
258,A,P,60,1200,by_fac
259,B,D,30,100,by_fac
260,B,D,60,1300,by_fac
425,B,E,10,64,by_fac
426,B,E,50,19,by_fac
427,B,P,20,2,by_fac
428,B,P,20,366,by_fac
433,generic_t1,generic_t2,10,98,gen
434,generic_t1,generic_t2,60,256,gen
435,generic_t1,generic_t2,10,78,gen
436,generic_t1,generic_t2,90,271,gen
441,generic_t1,generic_t2,15,48,gen
442,generic_t1,generic_t2,25,8,gen
") %>%
  mutate(treat1 = factor(treat1, levels=c("D", "E", "P", "generic_t2"), 
                         labels=c("D", "E", "P", " ")),
         treat2 = factor(treat2, levels=c("A", "B", "generic_t1"), 
                         labels=c("A", "B", " ")))
ggplot(data.a, 
       aes(x = temperature, y = response)) +
  
  geom_line(aes(color= treat1,
                linetype = treat2))+  
  
  scale_color_manual(values = c("red", "green", "orange", "black"),
                     labels=c("D", "E", "P", ""),
                     guide = guide_legend(override.aes=list(linetype=c(1,1,1,0), 
                                                            color = c("red", "green", "orange", "transparent")))) +

  scale_linetype_manual(values=c("solid", "dotted", "solid"),
                        labels=c("A", "B", ""), 
                        guide=guide_legend(override.aes=list(color=c("black", "black", "transparent")))) +
  
  theme_bw() +
  facet_wrap(~model,
             ncol = 1)

创建于2023-09-30使用reprex v2.0.2

相关问题