为什么geom_hline不会在ggplot2中生成图例?

时间:2015-09-28 23:23:17

标签: r ggplot2 legend

我有一些代码绘制了一些值的直方图,以及一些水平线来表示要比较的参考点。但是,ggplot没有生成行的图例。

library(ggplot2)
library(dplyr)

## Siumlate an equal mix of uniform and non-uniform observations on [0,1]
x <- data.frame(PValue=c(runif(500), rbeta(500, 0.25, 1)))
y <- c(Uniform=1, NullFraction=0.5) %>% data.frame(Line=names(.) %>% factor(levels=unique(.)), Intercept=.)
ggplot(x) +
    aes(x=PValue, y=..density..) + geom_histogram(binwidth=0.02) +
    geom_hline(aes(yintercept=Intercept, group=Line, color=Line, linetype=Line),
               data=y, alpha=0.5)

我甚至尝试将问题简化为仅绘制线条:

ggplot(y) +
    geom_hline(aes(yintercept=Intercept, color=Line)) + xlim(0,1)

我仍然没有得到传奇。任何人都可以解释为什么我的代码不会产生带有图例的情节吗?

1 个答案:

答案 0 :(得分:5)

EditText edit_for_user_name = (EditText) findViewById(R.id.editText1); String user_name = edit_for_user_name.getText().toString(); TextView text_view_for_house_choosing = (TextView) findViewById(R.id.textView1); String text_view_for_house_choosing_memory = text_view_for_house_choosing.getText().toString(); String text_view_for_house_choosing_all_in_one = text_view_for_house_choosing_memory + user_name; text_view_for_house_choosing.setText(text_view_for_house_choosing_all_in_one + user_name); 默认为EditText edit_for_user_name = (EditText) findViewById(R.id.editText1); String user_name = edit_for_user_name.getText().toString(); TextView text_view_for_house_choosing = (TextView) findViewById(R.id.textView1); text_view_for_house_choosing.setText(text_view_for_house_choosing.getText() + user_name); 。如果启用此选项,则会出现图例。此外,alpha需要在show_guide = FALSE内,否则线条的颜色将无法正确绘制(在图例上)。代码如下所示:

geom_hline

输出:

enter image description here