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

标签 r ggplot2 legend

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

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

但这会删除两个标题。必须有一种更清洁的方法来做到这一点。有谁知道吗?

最佳答案

添加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

关于r - 如何删除 ggplot 中多余的图例(标题)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67888600/

相关文章:

r - R - S 和 F 计数中的 bestglm 问题不能 <0

java - 从文本文件中命名 R 中的变量

R 软件包未安装在 Rstudio 服务器上

带重组的 R 对角网络(不仅仅是树的分离)

r - 使用 ggplot2 绘制误差线时抑制一些交叉线

r - 用于在 ggplot2 中绘图的归一化系列

r - 包装 R 的绘图函数(或 ggplot2)以防止绘制大数据集

python - 等高线图图例 - Matplotlib

r - 在循环中运行箱形图/水平线时,hline 索引没有改变的问题