使用ggplot2我怎样才能在图例中表示一个点和一条线 [英] Using ggplot2 how can I represent a dot and a line in the legend

查看:323
本文介绍了使用ggplot2我怎样才能在图例中表示一个点和一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot2我绘制了几个函数和一系列点。我无法弄清楚如何表示图例上的点。我意识到我需要使用aes()函数,但我不完全理解如何执行此操作。我很抱歉,这个例子很长,但我不知道怎么说明。

  ## add ggplot2 
library(ggplot2)

#声明图表值
y_label =表达式(y_axis~~ bgroup((,val / km ^ {2},)) )
x_label =x_axis

#############################
##定义函数
#创建一个列表来保存函数
funcs< - list()
funcs []

#循环定义函数
for(k in 1:21){

#使函数名称
funcName< - paste('func',k,sep ='')

#make function
func = paste('function(x){exp(',k,')* exp(x * 0.01)}',sep ='')

funcs [funcName]] = eval(parse(text = func))

}

#指定值
yval = c(1:20)
xval = c(1:20)

#创建数据框
d = data.frame(xval,yval)

#指定范围
x_range < - 范围(1,51)

#结果图
p <-qplot(data = d,
x = xval,y = yval,
xlab = x_label,
ylab = y_label,
xlim = x_range
)+ geom_point(color =green)


for(j in 1:长度(funcs)){

p <-p + stat_function(aes(y = 0),fun = funcs [[j]],color =blue,alpha = I(1/5 ))

}

#使一个函数红色
p < - p + stat_function(fun = funcs [[i]],aes(color =red ),size = 1)+
scale_colour_identity(,breaks = c(red,green,blue),
labels = c(拟合值,测量值,所有值))

#位置图例和删除帧
p < - p + opts(legend.position = c(0.85,0.7),legend.background = theme_rect (col = 0))

print(p)

谢谢前进 - 在过去几天里,我从这个社区学到了很多东西。

寻求解决方案。主要思想如下:想象点下方有一条看不见的线条,线条有不可见的点。因此,每个系列都会获得颜色和形状以及线型属性,最后我们会根据需要手动将它们设置为不可见值(线条为0,点为NA)。

 #make plot 
p < - qplot(data = d ,x = xval,y = yval,color =Measured,shape =Measured,
linetype =Measured,xlab = x_label,ylab = y_label,xlim = x_range,
geom = (a)(color =All)

#为功能添加行
(1中的长度为length(funcs)){
p < - p + stat_function ,shape =All,linetype =All),
fun = funcs [[j]],alpha = I(1/5),geom =line)
}

#使一个函数特殊
p <-p + stat_function(fun = funcs [[1]],aes(color =Fitted,shape =Fitted,
linetype = fitted),size = 1,geom =line)

#修改look
p <-p + scale_colour_manual(,values = c(green,blue ,red))+
scale_shape_manual(,values = c(19,NA,NA))+
scale_linetype_manual(,values = c(0,1,1))

print(p)


Using ggplot2 I am plotting several functions and a series of points. I cannot figure out how to represent the points on the legend. I realize I need to use an aes() function, but I don't fully understand how to do this. I apologize that the example is so long, but I don't know how else to illustrate it.

## add ggplot2
library(ggplot2)

# Declare Chart values
y_label = expression("y_axis"~~bgroup("(",val / km^{2},")"))
x_label = "x_axis"

#############################
## Define functions
# Create a list to hold the functions
funcs <- list()
funcs[]

# loop through to define functions
for(k in 1:21){

# Make function name
funcName <- paste('func', k, sep = '' )

# make function
func = paste('function(x){exp(', k, ') * exp(x*0.01)}', sep = '')

funcs[[funcName]] = eval(parse(text=func))

}

    # Specify values
    yval = c(1:20)                              
    xval = c(1:20)                                

    # make a dataframe
    d = data.frame(xval,yval)

    # Specify Range
    x_range <- range(1,51)

# make plot
p <-qplot(data = d,
        x=xval,y=yval,        
        xlab = x_label, 
        ylab = y_label,
        xlim = x_range
        )+ geom_point(colour="green")


for(j in 1:length(funcs)){

p <- p + stat_function(aes(y=0),fun = funcs[[j]], colour="blue", alpha=I(1/5))

}

# make one function red
p <- p + stat_function(fun = funcs[[i]], aes(color="red"), size = 1) +
    scale_colour_identity("", breaks=c("red", "green","blue"),
    labels=c("Fitted Values", "Measured values","All values")) 

# position legend and make remove frame
p <- p + opts(legend.position = c(0.85,0.7), legend.background = theme_rect(col = 0)) 

print(p)     

Thank you in advance - I have learned I a lot from this community over the last few days.

解决方案

See below for a solution. The main idea is the following: imagine the points having an invisible line under them, and the lines having invisible points. So each "series" gets color and shape and linetype attributes, and at the end we will manually set them to invisible values (0 for lines, NA for points) as necessary. ggplot2 will merge the legends for the three attributes automatically.

# make plot 
p <- qplot(data = d, x=xval, y=yval, colour="Measured", shape="Measured",
          linetype="Measured",  xlab = x_label,   ylab = y_label, xlim = x_range,
          geom="point") 

#add lines for functions 
for(j in 1:length(funcs)){ 
   p <- p + stat_function(aes(colour="All", shape="All", linetype="All"), 
                          fun = funcs[[j]],  alpha=I(1/5), geom="line")  
} 

# make one function special 
p <- p + stat_function(fun = funcs[[1]], aes(colour="Fitted", shape="Fitted",
                       linetype="Fitted"), size = 1, geom="line")

# modify look 
 p <- p +  scale_colour_manual("", values=c("green", "blue", "red")) + 
           scale_shape_manual("", values=c(19,NA,NA)) + 
           scale_linetype_manual("", values=c(0,1,1)) 

print(p) 

这篇关于使用ggplot2我怎样才能在图例中表示一个点和一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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