如何使用 directlabels 和 ggplot2?
我正在尝试使用 directlabels 包来标记我在一个简单的代码中的两行绘图(我正在使用 ggplot2)
我的代码如下:
# libraries
library(ggplot2)
library(directlabels)
# Variables
A = array(1000,100)
F = seq(length=100, from=0, by=10)
f = array(5,100)
# make data frame 1
df <- data.frame(X = F * f/A, Y = F/A)
# plot line 1
p = ggplot(df, aes(x=X,y=Y))
p = p + geom_line(colour="#56B4E9")
# make data frame 2
df1 <- data.frame(X = F * f * 2/A, Y = F/A)
# plot line 2
p = p + geom_line(aes(x=X,y=Y), data=df1, colour="#56B4E9")
# label line
direct.label(p, 'last.points')
但是我收到以下错误消息:
Error in direct.label.ggplot(p, "last.points") :
Need colour aesthetic to direct label.
我尝试向 < 添加几个参数code>direct.label() 函数,但我不明白应该使用什么美学参数。
I'm trying use the directlabels package to label two lines I have in a simple plot (I'm using ggplot2)
My code is as follows:
# libraries
library(ggplot2)
library(directlabels)
# Variables
A = array(1000,100)
F = seq(length=100, from=0, by=10)
f = array(5,100)
# make data frame 1
df <- data.frame(X = F * f/A, Y = F/A)
# plot line 1
p = ggplot(df, aes(x=X,y=Y))
p = p + geom_line(colour="#56B4E9")
# make data frame 2
df1 <- data.frame(X = F * f * 2/A, Y = F/A)
# plot line 2
p = p + geom_line(aes(x=X,y=Y), data=df1, colour="#56B4E9")
# label line
direct.label(p, 'last.points')
However I get the following error message:
Error in direct.label.ggplot(p, "last.points") :
Need colour aesthetic to direct label.
I've tried adding several arguments to the direct.label()
function, but I don't understand what aesthetic argument should be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以组合并融合它们,而不是使用 2 个数据框:
如果您想要直接标签,但不想使用颜色美感,您还可以使用 directlabels 2.0 中的新 geom_dl:
Instead of using 2 dataframes, you could combine and melt them:
You can also use the new geom_dl from directlabels 2.0 if you want direct labels, but don't want to use the colour aesthetic: