Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 472
djq Avatar asked Feb 12 '10 16:02

djq


People also ask

Why does my Ggplot not have a line?

The answer is that the X variable is a factor type.

How do you make a legend in ggplot2?

Adding a legend If you want to add a legend to a ggplot2 chart you will need to pass a categorical (or numerical) variable to color , fill , shape or alpha inside aes . Depending on which argument you use to pass the data and your specific case the output will be different.

How do I specify legend titles in ggplot2?

Another way to change legend titles is to use guides() function in ggplot2. Here, guides() function can take two legend titles as arguments. We use guide_legend() to specify the new title we want one for size and other for color.


1 Answers

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) 
like image 109
Aniko Avatar answered Sep 30 '22 01:09

Aniko