你怎么能使用直接标签和ggplot2? [英] How can you use directlabels and ggplot2?

查看:162
本文介绍了你怎么能使用直接标签和ggplot2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 directlabels 包裹来标注两行在一个简单的情节(我使用 gplot2




我的代码如下:

 #库
库(ggplot2)
库(直接标签)

#变量
A = array(1000,100)
F = seq(length = 100,from = 0,by = 10)
f = array(5,100)

#使数据帧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(颜色= #56B4E9)

#使数据帧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,color =#56B4E9)

#标签行
direct.label(p ,'last.points')

但是,我收到以下错误消息:

  direct.label.ggplot(p,last.points)中的错误:
需要颜色唯美来指示标签。

我试着在 direct.label() / code>函数,但我不明白应该使用什么美学参数。

解决方案

dataframes,你可以合并和融合它们:

pre $ $ $ $ $ $ $ $
#变量
A =数组(1000,100)
F = seq(长度= 100,from = 0,by = 10)
f = array(5,100)

#make数据帧1
df< - data.frame(X = F * f / A,Y = F / A)

#使数据帧2
df1 < - data.frame(X = F * f * 2 / A,Y = F / A)

#合并两个数据帧
df2< - merge(df, df1,by =X)

#将它们融化
df2m < - melt(df2,id =X)

#plot
P2< - ggplot(df2m,AES(X = X,Y =值,列=可变))+
geom_line()+
scale_color_manual(值=代表( #56B4E9,2) )

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

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

  install.packages( directlabels)
ggplot(df2m,AES(X = X,Y =值))+
geom_line(AES( group = variable))+
geom_dl(aes(label = variable),method =last.points)


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.

解决方案

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")

这篇关于你怎么能使用直接标签和ggplot2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆