如何使用 directlabels 和 ggplot2?

发布于 2024-11-17 06:48:06 字数 1001 浏览 0 评论 0原文

我正在尝试使用 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)

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-11-24 06:48:06

您可以组合并融合它们,而不是使用 2 个数据框:

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)

# make data frame 2
df1 <- data.frame(X = F * f * 2/A, Y = F/A)

# merge both dataframes
df2 <- merge(df, df1, by = "X")

# melt them
df2m <- melt(df2, id = "X")

# plot 
p2 <- ggplot(df2m, aes(x = X, y = value, col = variable)) +
  geom_line() +
  scale_color_manual(values = rep("#56B4E9", 2))

direct.label(p2, 'last.points')

如果您想要直接标签,但不想使用颜色美感,您还可以使用 directlabels 2.0 中的新 geom_dl:

install.packages("directlabels")
ggplot(df2m, aes(x = X, y = value))+
  geom_line(aes(group=variable))+
  geom_dl(aes(label=variable),method="last.points")

标记的 ggplot

Instead of using 2 dataframes, you could combine and melt them:

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)

# make data frame 2
df1 <- data.frame(X = F * f * 2/A, Y = F/A)

# merge both dataframes
df2 <- merge(df, df1, by = "X")

# melt them
df2m <- melt(df2, id = "X")

# plot 
p2 <- ggplot(df2m, aes(x = X, y = value, col = variable)) +
  geom_line() +
  scale_color_manual(values = rep("#56B4E9", 2))

direct.label(p2, 'last.points')

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:

install.packages("directlabels")
ggplot(df2m, aes(x = X, y = value))+
  geom_line(aes(group=variable))+
  geom_dl(aes(label=variable),method="last.points")

labeled ggplot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文