Can you add labels below the existing axis?

I am using ggplot2 in r and made my plot. I would like to add some spacing that I have in the CSV file under my existing x-axis.

Here is an example to show what I would like to plot under my x-axis: http://www.nature.com/nprot/journal/v8/n5/images/nprot.2013.053-F3.jpg Image "a" is spaced under the x-axis. I have. CSV file at intervals in the format:

Fraction no.; Interval
"1"; [0:2]
"2"; [2:4]

      

and so on, up to about 80, so I would like them to be added automatically. I hope this is possible so I don't have to do it manually in another program.

+3


source to share


2 answers


We can use geom_segment

from ggplot

to draw intervals in a new drawing and rearrange your graph with help grid.arrange()

from the gridExtra

library.

For example, with aperture: I created a histogram:

g <- ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+geom_histogram(stat="identity")

      

Then with interval data like this:

inter <- data.frame(v1=seq(0,max(iris$Sepal.Length),1.5),v2=seq(1,max(iris$Sepal.Length)+2,1.5))

head(inter)
   v1  v2
1 0.0 1.0
2 1.5 2.5
3 3.0 4.0
4 4.5 5.5
5 6.0 7.0
6 7.5 8.5

      

I created a new graph with no background and axis:



top <- ggplot(inter,aes(v1,0))+geom_segment(aes(x=v1,y=0,xend=v2,yend=0))+geom_segment(aes(x=v1,xend=v1,y=0,yend=0.5))+geom_segment(aes(x=v2,xend=v2,y=0,yend=0.5))+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(),axis.ticks=element_blank(),axis.text=element_blank())+xlab("")+ylab("")

      

And add it to the previous plot

grid.arrange(g,top,ncol=1,nrow=2,heights=c(6,1))

      

This is the result:

enter image description here

+4


source


I chose a different solution using annotation and making an area inside the graph, for example:

annotate("rect", xmin = 10, xmax = 30, ymin = -Inf, ymax = Inf, fill = "grey70", alpha = 0.3) +
annotate("text", x = 20, y = -65, color = "black", label = "3-6", size = 3) +
annotate("rect", xmin = 78, xmax = 94, ymin = -Inf, ymax = Inf, fill = "grey70", alpha = 0.3) +
annotate("text", x = 86, y = -65, color = "black", label = "24-39", size = 3)

      



Result in this plot: Plot with areas marked with annotate

+1


source







All Articles