0

Running in R this code:

# example data

x = runif(14, 0.0, 1.0)
norm = x/sum(x)
data = data.frame(replicate(17,sample(norm)))

require(ggplot2)
require(reshape2)

data$no <- seq_len(nrow(data))
data_molten <- melt(data, id.vars = "no")
data_molten[data_molten == 0] <- NA
View(data_molten)

# ggplot

ggplot(data_molten[!is.na(data_molten$value),], aes(x = variable, y = value, fill = factor(no))) + 
    geom_bar(stat = "identity") + 
    scale_fill_hue(l=40) + facet_grid(no~.) + theme_minimal() +
    theme(legend.position="none", 
          axis.text.x = element_text(angle = 90, colour="black", vjust = 0.5, hjust=1, size=16), 
          axis.title.x = element_blank(), axis.title.y = element_blank(), 
          axis.line.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), 
          strip.text.y=element_text(size = 16, colour="black", family="", angle=00,hjust = 0),
          panel.grid=element_blank(),
          axis.line=element_line(size = 1, colour = "black", linetype = "solid"),
          axis.ticks.x=element_line(size = 1, colour = "black", linetype = "solid"),
          panel.background=element_blank())

produces this plot:

enter image description here

Anybody knows how can I add a vertical line between the right side of the plot and the layers' labels (the 1, 2, 3, ..., 14) which have a tick per layer's label, keeping enough space between the plot, the axis and the labels?

EDIT:

Running

num_var_x = 14
num_var_y = 17

x = runif(num_var_x, 0.0, 1.0)
norm = x/sum(x)
data = data.frame(replicate(num_var_y,sample(norm)))

## preparing dataset for ggplot

require(ggplot2)
require(reshape2)
data$no <- seq_len(nrow(data))
data_molten <- melt(data, id.vars = "no")

data_molten_sort = data_molten[with(data_molten,order(no)),]

## removing elements from variable 'no' whose max. value is e.g. < 0.025

sequence = seq(from=1, to=(num_var_y*num_var_x-num_var_x)+1, by=num_var_x)
for(i in 1:length(sequence))
{
    if(isTRUE((max(data_molten_sort$value[(sequence[i]):((num_var_x+sequence[i])-(1))])) < 0.025))
    {
            data_molten_sort$value[(sequence[i]):((num_var_x+sequence[i])-(1))] = NA
    }
}

View(data_molten)


## preparing posterior exporting

#install.packages("Cairo"); "cairo" type in png() has a better quality
library("Cairo")
#preparing exporting
png(file="ggplot.png",type="cairo", width = 4, height = 5, units = 'in',pointsize=8,res=600)

## plotting

ggplot(data_molten[!is.na(data_molten$value),], aes(x = variable, y = value, fill = factor(no))) + 
    geom_bar(stat = "identity") + 
    scale_fill_hue(l=40) + facet_grid(no~., as.table=FALSE, scale="free_y", space = "free_y") + theme_minimal() +
    geom_vline(xintercept=max(as.numeric(data_molten$variable)) + 0.586, size=0.3) +
    theme(legend.position="none", 
          axis.text.x = element_text(angle = 90, colour="black", vjust = 0.4, hjust=1, size=8), 
          axis.title.x = element_blank(), axis.title.y = element_blank(), 
          axis.line.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank(), 
          strip.text.y=element_text(size = 8, colour="black", family="", angle=00,hjust = 0.1),
          panel.grid=element_blank(),
          axis.line=element_line(size = 0.3, colour = "black", linetype = "solid"),
          axis.ticks.x=element_line(size = 0.3, colour = "black", linetype = "solid"),
          panel.background=element_blank(), panel.margin = unit(0, "lines"))

## exporting barplot "ggplot.png" to directory
dev.off()

now the y line is continuous, plus other additions such as an exporting command.

https://i.stack.imgur.com/RnAa4.png

Miguel
  • 356
  • 1
  • 15
  • Possible duplicate of [this](http://stackoverflow.com/questions/23527385/place-y-axis-on-the-right-of-a-ggplot) or [this](http://stackoverflow.com/questions/15334494/how-to-change-positions-of-x-and-y-axis-in-ggplot2) or [this?](http://stackoverflow.com/questions/18989001/how-can-i-put-a-transformed-scale-on-the-right-side-of-a-ggplot2) – Andrew Taylor Nov 25 '15 at 12:47
  • 1
    Does ... `+ geom_vline(xintercept=max(as.numeric(data_molten$variable))+0.5, size=1)` do what you want? – Heroka Nov 25 '15 at 12:50
  • Thank you Heroka, that works quite well, however I would like the line to be continuous across the layers, and this line is divided between layers (http://i.imgur.com/xTY8IP2.png?1), is there any option in this argument to change this? Also, how could I put a tick for each number (1...14)? – Miguel Nov 25 '15 at 14:52

1 Answers1

0

Assuming that your original plot is saved in an object p1, this could be a solution:

p1 +  geom_vline(xintercept=max(as.numeric(data_molten$variable))+0.5, size=1) + #data-dependent
  scale_x_discrete(expand=c(0,0))+ #create nicer corner
  theme(
    #move labels slightly to the right with hjust
    strip.text.y=element_text(size = 16, colour="black", family="", angle=00,hjust = 0.5) 
    )

enter image description here

Heroka
  • 12,889
  • 1
  • 28
  • 38
  • These are all improvements indeed, thank you, but what I want is to have a continuous line, not splitted in 14 parts one per layer, and also to add to the line a tick per layer, so 14 ticks. – Miguel Nov 25 '15 at 15:32
  • I don't know how to make the line continuous, and y-axis (with ticks) on the right side are notoriously difficult in ggplot (with reason). Plus, what would the ticks signify in this case? Imho they are unnessecary. – Heroka Nov 25 '15 at 15:48
  • Ok, the ticks were just for aesthetics, nevermind, thank you again! – Miguel Nov 25 '15 at 20:17