简体   繁体   English

ggplot2中的其他图例

[英]Additional legend in ggplot2

I have a problem where the legend of my ggplot() does not appear. 我有一个问题,我的ggplot()的图例没有出现。 Here's my code: 这是我的代码:

plot_bt <- ggplot(NULL, aes(x, v1)) + 
  geom_line(data = nig_bt_1, colour = "black") +
  geom_line(data = nig_bt_2, colour = "blue") +
  geom_line(data = nig_bt_3, colour = "red") + 
  labs(x = "X", y = "Probability")

I tried to make a legend inside this graph but I could not do it. 我试图在此图中创建图例,但我做不到。 It just does not appear. 它只是没有出现。 I try to make a plot of three different types of NIG distribution. 我尝试绘制三种不同类型的NIG分布图。 In nig_bt_1 etc. I have my values. 在nig_bt_1等中,我有我的值。 Those three densities appear but the legend doesn't. 这三个密度出现,但图例没有。 I tried the scale_color_manual function too with no success. 我也尝试过scale_color_manual函数,但没有成功。

Thank you very much. 非常感谢你。

x <- seq(-7.5,7.5,0.001)
nig_bt_1 <- data.frame(x ,v1 = dnig(x, param = pr_bt_1))
nig_bt_2 <- data.frame(x ,v1 = dnig(x, param = pr_bt_2))
nig_bt_3 <- data.frame(x ,v1 = dnig(x, param = pr_bt_3))

Just do this: 只要这样做:

plot_bt <- ggplot(NULL, aes(x, v1)) + 
  geom_line(data = nig_bt_1, aes(colour = "a")) +
  geom_line(data = nig_bt_2, aes(colour = "b")) +
  geom_line(data = nig_bt_3, aes(colour = "c")) + 
  labs(x = "X", y = "Probability") +
  scales_color_manual(values= c("a" = "black", "b" = "blue", "c" = "red"))

A guide can only depict mappings you've defined using aes . 指南只能描述您使用aes定义的映射。 The ggplot2 way is of course to first combine the data and use a grouping variable. ggplot2方法当然是首先组合数据并使用分组变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM