R 为什么在ggplot2中手动编辑时会出现两个图例?

R 为什么在ggplot2中手动编辑时会出现两个图例?,r,ggplot2,R,Ggplot2,我想画两条线,一条是实线,另一条是虚线,都有不同的颜色。我在处理这个情节的传奇方面遇到了麻烦。举个例子: library(ggplot2) library(reshape2) df = data.frame(time = 0:127, mean_clustered = rnorm(128), mean_true = rnorm(128) ) test_data_long <- melt(df, id="time") # c

我想画两条线,一条是实线,另一条是虚线,都有不同的颜色。我在处理这个情节的传奇方面遇到了麻烦。举个例子:

library(ggplot2)
library(reshape2)

df = data.frame(time = 0:127,
                mean_clustered = rnorm(128),
                mean_true = rnorm(128)
)
test_data_long <- melt(df, id="time")  # convert to long format
p = ggplot(data=test_data_long,
           aes(x=time, y=value, colour=variable)) +
  geom_line(aes(linetype=variable)) +
  labs(title = "", x = "Muestras", y = "Amplitud", color = "Spike promedio\n") +
  scale_color_manual(labels = c("Hallado", "Real"), values = c("blue", "red")) +
  xlim(0, 127)

print(p)
库(ggplot2)
图书馆(E2)
df=数据帧(时间=0:127,
平均值=rnorm(128),
平均值=标准(128)
)
test_data_long您需要确保所使用的不同美学之间的所有美学映射匹配:

library(ggplot2)
library(reshape2)

data.frame(
  time = 0:127,
  mean_clustered = rnorm(128),
  mean_true = rnorm(128)
) -> xdf

test_data_long <- melt(xdf, id = "time")

ggplot(
  data = test_data_long,
  aes(x = time, y = value, colour = variable)
) +
  geom_line(aes(linetype = variable)) +
  scale_color_manual(
    name = "Spike promedio\n", labels = c("Hallado", "Real"), values = c("blue", "red")
  ) + 
  scale_linetype(
    name = "Spike promedio\n", labels = c("Hallado", "Real")
  ) +
  labs(
    x = "Muestras", y = "Amplitud", title = ""
  ) +
  xlim(0, 127)
ggplot(data = test_data_long, aes(x = time, y = value, colour = variable)) +
  geom_line(aes(linetype = variable)) +
  scale_x_continuous(name = "Muestras", limits = c(0, 127)) +
  scale_y_continuous(name = "Amplitud") +
  scale_color_manual(name = "Spike promedio", labels = c("Hallado", "Real"), values = c("blue", "red")) + 
  scale_linetype(name = "Spike promedio", labels = c("Hallado", "Real")) +
  labs(title = "") +
  theme(legend.title = element_text(margin = margin(b=15)))