将自定义图例添加到 ggplot2 中的 2 个数据集

Adding Custom Legend to 2 Data sets in ggplot2

我想简单地在我绘制 2 组数据的 Nyquist 图中添加图例:1 是实验集(~600 点),2 是使用传递函数计算的数据框(~ 1000 点)

我需要绘制两者并标记它们。目前我把它们都绘制好了,但是当我尝试使用 scale_colour_manual 添加标签时,没有出现标签。也将不胜感激移动此标签的方法!!下面的代码。

    pdf("nyq_2elc.pdf")

  nq2 <- ggplot() + geom_point(data = treat, aes(treat$V1,treat$V2), color = "red") +
    geom_point(data = circuit, aes(circuit$realTF,circuit$V2), color = "blue") +
    xlab("Real Z") + ylab("-Imaginary Z") + 
    scale_colour_manual(name = 'hell0', 
                       values =c('red'='red','blue'='blue'), labels = c('Treatment','EQ')) +
    ggtitle("Nyquist Plot and Equivilent Circuit for 2 Electrode Treatment Setup at 0 Minutes") +
    xlim(0,700) + ylim(0,700) 


print(nq2)
dev.off()

Ggplot 最适合长数据帧,所以我会像这样组合数据集:

treat$Cat <- "treat"
circuit$Cat <- "circuit"
CombData <- data.frame(rbind(treat, circuit))

ggplot(CombData, aes(x=V1, y=V2, col=Cat))+geom_point()

这应该会给您想要的图例。

您可能必须更改数据框 treatcircuit 列的 names/order 以便它们可以组合,但很难说,因为您没有给我们一个可重现的例子。