I'd like to create a plot with a quadratic curve in black, and horizontal lines at various levels in different colours, with a legend (guide) that labels the horizontal line colours. I can't quite figure it out -- I've tried a couple of variations that seem to make sense to me, but I can't seem to force the legend to appear.
我想用黑色的二次曲线和不同颜色的不同水平的水平线来绘制一幅图,上面有一个标记水平线颜色的图例(指南)。我不太明白——我试过了一些对我有意义的变奏,但我似乎不能强迫传奇出现。
Here's my latest attempt:
这是我的最新尝试:
library(ggplot2)
theme_set(theme_bw()) ## cosmetic
hdat <- data.frame(harvest_rate=c(5,15,25,30))
r <- 1; K <- 100
ggplot(hdat)+
geom_hline(data=hdat,aes(yintercept=harvest_rate,
colour=factor(harvest_rate)))+
stat_function(fun=function(x) r*x*(1-x/K),colour="black")+
expand_limits(x=c(0,110))
The lines come out OK, but the legend/guide is not there.
台词不错,但传说/指南不在那里。
If I do this instead:
如果我这样做:
ggplot(hdat,aes(yintercept=harvest_rate,
colour=factor(harvest_rate)))+
geom_hline()+
stat_function(fun=function(x) r*x*(1-x/K),colour="black")+
expand_limits(x=c(0,110))
then to my surprise the horizontal lines don't get drawn at all!
让我惊讶的是,水平线根本没有被画出来!
I've also tried (I started this way) setting up a data frame with x
and y
variables,
我也尝试过(我是这样开始的)用x和y变量建立一个数据框架,
d <- data.frame(x=0:110)
d <- transform(d,y=r*x*(1-x/K))
ggplot(d,aes(x,y))+geom_line()+
geom_hline(data=hdat,aes(yintercept=harvest_rate,
colour=factor(harvest_rate)))+
scale_colour_brewer(palette="Set1")
The guide doesn't show up that way either.
指南也没有这样显示。
If I set colour=NA
in the initial ggplot
call the legend appears, but the curve disappears. If I set colour=factor(1)
as follows
如果我在初始的ggplot中设置颜色=NA,就会出现传奇,但曲线会消失。如果我设置颜色=因子(1)如下所示
ggplot(d,aes(x,y,colour=factor(1)))+geom_line()+
geom_hline(data=hdat,aes(yintercept=harvest_rate,
colour=factor(harvest_rate)))+
scale_colour_brewer(palette="Set1")
I get a curve and a legend, but the curve is in a bogus colour. If I override by setting geom_line(colour="black")
then the legend disappears again ...
我得到了一个曲线和一个传说,但是曲线是假的颜色。如果我通过设置地_line(颜色=“黑色”)来覆盖,那么这个传奇将再次消失。
I would be very grateful for (1) a hack that works and (2) an explanation of the logic that I'm missing!
我将非常感激(1)一个有效的黑客和(2)一个解释我错过的逻辑!
1 个解决方案
#1
3
The complete answer:
完整的回答:
library(ggplot2)
theme_set(theme_bw()) ## cosmetic
hdat <- data.frame(harvest_rate=c(5,15,25,30))
r <- 1; K <- 100
ggplot(hdat)+
geom_hline(aes(yintercept=harvest_rate, colour=factor(harvest_rate)), show_guide=TRUE)+
stat_function(fun=function(x) r*x*(1-x/K),colour="black")+
expand_limits(x=c(0,110)) +
labs(colour = "Harvest rate") # making a pretty legend title
The result:
结果:
#1
3
The complete answer:
完整的回答:
library(ggplot2)
theme_set(theme_bw()) ## cosmetic
hdat <- data.frame(harvest_rate=c(5,15,25,30))
r <- 1; K <- 100
ggplot(hdat)+
geom_hline(aes(yintercept=harvest_rate, colour=factor(harvest_rate)), show_guide=TRUE)+
stat_function(fun=function(x) r*x*(1-x/K),colour="black")+
expand_limits(x=c(0,110)) +
labs(colour = "Harvest rate") # making a pretty legend title
The result:
结果: