Question

Ive worked on this from a previous post: Combined line & bar geoms: How to generate proper legend? And have gotten close. Here is the code I used which adds a line and point geom to the bar plot:

  mort12=data.frame(
     Adj.S=c(.68,.33,.66,.62,.6,.51,.6,.76,.51,.5),
     QTL=c(1:10),
     Cum.M=c(.312,.768,NA,.854,NA,.925,.954,NA,NA,.977)
)

  ggplot(data=mort12, aes(QTL)) + 
  geom_bar(aes(y = Adj.S, color = "Adj.S"), stat="identity", fill = "red") +
  geom_point(data=mort12[!is.na(mort12$Cum.M),], aes(y = Cum.M, group = 1,size=4, color =    "Cum.M"))+  
  geom_line(data=mort12[!is.na(mort12$Cum.M),],aes(y=Cum.M, linetype="dotted",group = 1))

(Note, I have some missing data for Cum.M, so to connect those points I added code to ignore the missing values).

And when I run this, I get this figure (I cant post pictures here, so its linked): https://docs.google.com/uc?export=view&id=0B-6a5UsIa6UpZnRZTy1OZmxrY1E

Id like to control the appearance of the line and points. But attempts to make the line dotted (linetype="dotted") did not change it, and when I attempt to change the fill of the dots (fill="white") I ge this error

Error: A continuous variable can not be mapped to shape

Any suggestions on how to alter the attributes of the line and points?

Was it helpful?

Solution

This worked for me:

ggplot(data=mort12, aes(QTL)) + 
  geom_bar(aes(y = Adj.S, color = "Adj.S"), stat="identity", fill = "white") +
  geom_point(data=mort12[!is.na(mort12$Cum.M),], aes(y = Cum.M, group = 1,size=4, color =    "Cum.M"))+  
  geom_line(data=mort12[!is.na(mort12$Cum.M),],aes(y=Cum.M, group = 1), linetype="dotted")

enter image description here

All I did was move linetype outside of aes. Generally speaking, aesthetics that are not driven by your data should not be inside aes. For example, size should probably also not be in aes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top