如何在ggplot中删除多余的图例(标题)?

3
我试图删除“系列”图例,因为我添加了自己的图例(主要是为了更改图例标题)。 以下是我的代码(应该是可重复的)

p1 image output

df<-data.frame(
  "time"=c(100, 75, 50, 25, 0, -25),
  "nativ"=c("start", "75", "50", "25","home","end"),
  "series"=c("A", "A","A", "A", "A","A"),
  "value"=runif(n = 6,min = 0,max = 20)
)

serlabel=c("start", "75", "50", "25","home","end")

p2<-ggplot2::ggplot(
  df, 
  ggplot2::aes(time,
               value,
               group=1
  )) + 
  ggplot2::geom_line(
    ggplot2::aes(colour = series, linetype=series))+
  #
  ggplot2::labs(x= "Locations", 
                y="APC", 
                colour=paste(
                  "New legend title")
  )+ #add variiable to function if fpkm or fc
  ggplot2::scale_x_reverse(breaks = c(100, 75, 55, 35, 10, -20),
                           labels = serlabel, #enter serieslabel variable
                           expand=c(0,0))+
  ggplot2::theme_bw()
p2

我尝试通过添加代码来移除系列图例:
+ggplot2::theme(legend.title = ggplot2::element_blank())


但是这样做会同时移除两个标题,肯定有更简洁的方法。是否有人知道如何处理?

您确切想要什么结果?既显示颜色又显示线型的图例?只显示颜色而不显示线型的图例?还是其他什么东西? - camille
2个回答

2
添加 scale_linetype(guide = FALSE)
ggplot(df, aes(time, value, group=1)) + 
  geom_line(aes(colour = series, linetype=series)) +
  labs(x= "Locations", y="APC", colour= "New legend title") +
  scale_x_reverse(breaks = c(100, 75, 55, 35, 10, -20),
                           labels = serlabel, expand=c(0,0))+
  scale_linetype(guide = FALSE) +
  theme_bw()

enter image description here


1
在实验室中,对于“linetype”图例,使用相同的标题。
ggplot(df, aes(time, value)) +
  geom_line(
    aes(colour = series, linetype = series)
  ) +
  labs(
    x = "Locations", 
    y = "APC", 
    colour = "New legend title",
    linetype = "New legend title"
  ) +
  scale_x_reverse(
    breaks = c(100, 75, 55, 35, 10, -20),
    labels = serlabel, #enter serieslabel variable
    expand = c(0, 0)
  ) +
  theme_bw()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接