简体   繁体   English

使用ggplot2如何在图例中表示点和线

[英]Using ggplot2 how can I represent a dot and a line in the legend

Using ggplot2 I am plotting several functions and a series of points. 使用ggplot2我正在绘制几个函数和一系列点。 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. 我意识到我需要使用aes()函数,但我不完全理解如何做到这一点。 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. 因此,每个“系列”都会获得颜色,形状和线型属性,最后我们将根据需要手动将它们设置为不可见值(线条为0,点为NA)。 ggplot2 will merge the legends for the three attributes automatically. ggplot2将自动合并这三个属性的图例。

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

Setting the colour aesthetic for each geom to a constant may help. 将每个geom的颜色美学设置为常量可能会有所帮助。 Here is a small example: 这是一个小例子:

require(ggplot2)
set.seed(666)
N<-20
foo<-data.frame(x=1:N,y=runif(N),z=runif(N))
p<-ggplot(foo)
p<-p+geom_line(aes(x,y,colour="Theory"))
p<-p+geom_point(aes(x,z,colour="Practice"))

#Optional, if you want your own colours
p<-p+scale_colour_manual("Source",c('blue','red'))

print(p)

替代文字

ggplot2本身不支持这个,但我希望我能弄清楚未来的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM