Shading between 2 lines

36 views
Skip to first unread message

munasur12

unread,
Aug 20, 2015, 4:19:18 PM8/20/15
to ggplot2
Hi, 

I would like to shade between 2 lines. However, I am not able to shade the entire area between the line. Below is my code:
ambi.m<-matrix(data=NA,nrow=3,ncol=2)

colnames(ambi.m)<-c("Schmitt et al. Pipeline", "FHiTINGs Pipepline")
rownames(ambi.m)<-c("Family","Genus","Species")

ambi.m[,1]<-c(2220,2335,2455)
ambi.m[,2]<-c(221,370,639)

ambi.m.melt<-melt(ambi.m,id.vars=c(rownames(ambi.m),colnames(ambi.m)))
ambi.m.melt[,"value"]<-(ambi.m.melt[,"value"]/3317)
ambi.m.melt<-cbind(ambi.m.melt,c(1,2,3))
colnames(ambi.m.melt)[4]<-"num"
colnames(ambi.m.melt)[1:2]<-c("Taxon","pipeline")

p10<-ggplot()
p10<-p10+geom_line(data=ambi.m.melt,aes(x=num,y=as.numeric(value),group = pipeline,color=pipeline),size = 1)
p10<-p10+geom_polygon(data=as.data.frame(poly.y.values),aes(fill = "red"))
p10<-p10+scale_color_manual(values=c("#C0533A","#92B8B9"),guide_legend(title="Pipeline")) + xlab("Taxonomic Level") + ylab("") 
p10<-p10+theme(panel.background=element_rect(fill="white",color="black"),
             axis.text.y=element_blank(),axis.ticks=element_line(color="gray67"),
             panel.grid.major=element_line(color="gray91"),panel.grid.minor=element_blank(),
             axis.text.x = element_text(angle = 90, hjust = 1,color="black"))

I appreciate any insight.

Thanks for your help in advance,
Muna

Dennis Murphy

unread,
Aug 20, 2015, 8:02:38 PM8/20/15
to munasur12, ggplot2
Hi:

Firstly, your example is not completely reproducible because you did
not supply poly.y.values. Tip: It's easier if you create data frames
outside of ggplot2 first rather than try to construct them inline. At
the very least, the code will be better organized. You can shade the
area between the two lines with geom_ribbon(); however, you want to
keep the 'wide form' of ambi.m in order to do that. Removing the
geom_polygon() line in the ggplot call and making a few other changes,
the following appears to work:

ambi.m<-matrix(data=NA,nrow=3,ncol=2)

# You only need the full labels in the legend; see
scale_color_manual() for the change
# Keep variable names simple and avoid spaces, especially if you are
# going to modify with data.frame() or reshape2::melt()
colnames(ambi.m)<-c("Schmitt", "FHiTINGs")
rownames(ambi.m)<-c("Family","Genus","Species")

ambi.m[,1]<-c(2220,2335,2455)
ambi.m[,2]<-c(221,370,639)

# Load the packages
library(reshape2)
library(ggplot2)

# Melt ambi.m into long form
ambi.m.melt<-melt(ambi.m/3317,id.vars=c(rownames(ambi.m),colnames(ambi.m)))
ambi.m.melt<-cbind(ambi.m.melt, num = 1:3)
colnames(ambi.m.melt)[1:2]<-c("Taxon","pipeline")

# Convert ambi.m into a "wide form" data frame, adding a column for num
dflines <- data.frame(num = 1:3, as.data.frame(ambi.m/3317))

# Generate the plot
ggplot() +
geom_line(data = ambi.m.melt, aes(x = num, y = value, color = pipeline),
size = 1) +
geom_ribbon(data = dflines,
aes(x = num, ymax = Schmitt,
ymin = FHiTINGs),
fill = "grey60", alpha = 0.3) +
scale_color_manual(values=c("#C0533A","#92B8B9"),
labels = c("Schmitt et al. Pipeline",
"FHiTINGs Pipeline")) +
scale_x_continuous(breaks = 1:3) +
labs(x = "Taxonomic Level", y = NULL, color = "pipeline") +
theme(panel.background=element_rect(fill="white",color="black"),
axis.text.y=element_blank(),
axis.ticks=element_line(color="gray67"),
panel.grid.major=element_line(color="gray91"),
panel.grid.minor=element_blank())

Some comments re the plot:
(i) I used a darker fill color with alpha transparency in
geom_ribbon() so that the grid lines are a bit more visible - this
should go first because we normally want this as a background layer
rather than a foreground one;
(ii) I used the labels = argument of scale_color_manual() to provide
the full labels for the legend;
(iii) I used labs() to define the titles for each guide;
(iv) I got rid of the label rotation on the x-axis by defining breaks
as integer values in scale_x_continuous().

You probably want to position the polygon layer between geom_ribbon()
and scale_color_manual().

HTH,
Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

munasur12

unread,
Aug 26, 2015, 10:23:05 AM8/26/15
to ggplot2, anuradha...@gmail.com
Dennis,

Thank you very much for your help and feedback. You helped me figure out where I went wrong.


Muna
Reply all
Reply to author
Forward
0 new messages