Rudy
Rudy

Reputation: 387

r ggplot x axis color gradient labels/values

I currently have a plot graph with ggplot2 which has values associated on each coordinate which is expressed by a color gradient. My x values are ordered and is there anyway to express a separate gradient just for the x axis labels? For example

x    y   z
7.5  55  1000
7.5  20  2000
7.5  10  3000
3    70  2500
3    50  10000
3    25  1300

The graph is plotted with x,z with y being the gradient colour. I want to assign color gradient to x values along the x axis as shown in the image below.

enter image description here enter image description here

Something similar to the graph would be great!

The code i used is pasted below

plot_data=read.table("Finalfile.txt",sep="\t",header=T)
plot_data=plot_data[,1:8]
#order data according to expression followed by gene id
order_data=plot_data[order(plot_data$exp,plot_data$gene,decreasing=TRUE),]

#convert gene ids to numeric, same number for each gene id
plot_data=transform(plot_data,id=as.numeric(factor(gene)))

#x y plot 
ggplot(plot_data, aes(gene, TSS, colour=met)) +
  geom_point(shape=15) + ylim(0,10000)+
  scale_colour_gradient(low="blue", high="red")+theme(axis.text.x = element_text(angle = 90, hjust = 1))

Upvotes: 0

Views: 1955

Answers (1)

Feargal Ryan
Feargal Ryan

Reputation: 454

I've interpreted your request to mean you want to demonstrate the spread of data on the x-axis.

I think this could help.

require(ggplot2)
library(gridExtra)

x   <- c(7.5 ,7.5 ,7.5 ,3 ,3 ,3 )
z   <- c(1000,2000,3000,2500,10000,1300)
y   <- c(55  ,20  ,10  ,70  ,50  ,25  )

data <- data.frame(x,z,y)

main <-ggplot (data, aes( x = x, y = z, colour = y))+
    geom_point() +
    scale_color_gradient(limits = c(0, max(y)))+
    theme(legend.position=c(1,1),legend.justification=c(1,1))

density.x <- ggplot(data, aes(x), fill = "blue")+        
        geom_density(width = c(0, max(y)),  alpha = 0.5,binwidth =1,  position   = 'identity', fill = "blue")

grid.arrange(density.x,  main, ncol = 1, nrow =2 , heights= c(1,4))

This does not give the colouring you originally requested but still conveys the same information. You could experiment with different geoms for different representations.

See here for further expansion on the theme: http://www.r-bloggers.com/ggplot2-cheatsheet-for-visualizing-distributions/

Upvotes: 1

Related Questions