2017-05-25 36 views
2

添加y軸到截短barplot我有以下數據(4列和第6行數據):我已想出該R腳本,它產生了很好的截短barplot使用ggplot

w x y z 
a 0.02 0.00 0.60 0.19 
b 0.03 0.40 0.10 0.05 
c 0.00 0.01 0.66 0.26 
d 0.05 0.04 0.57 0.00 
e 0.20 0.60 0.15 0.00 
f 0.10 0.30 0.30 0.20 

通過使用ggplot:

## importing data 

plot <- read.delim("data", row.names=1) 
View(plot) 


### loading required packages 

if (!require("Rcpp")) { 
    install.packages("Rcpp", repos="http://cran.rstudio.com/") 
    library("Rcpp") 
} 
library("Rcpp") 

if (!require("ggplot2")) { 
    install.packages("ggplot2", repos="http://cran.rstudio.com/") 
    library("ggplot2") 
} 
library("ggplot2") 

if (!require("grid")) { 
    install.packages("grid", repos="http://cran.rstudio.com/") 
    library("grid") 
} 
library("grid") 


if (!require("reshape2")) { 
    install.packages("reshape2", repos="http://cran.rstudio.com/") 
    library("reshape2") 
} 
library(reshape2) 


## transpose "plot" 
plot = t(plot) 

plot <- melt(plot, id.vars=2:ncol(plot)) 


## running ggplot 

bars = ggplot(plot[!is.na(plot$value),], aes(x = Var2, y = value, fill = factor(Var1))) + 
    geom_bar(stat = "identity") + 
    scale_fill_manual(values=rainbow(length(levels(plot$Var1)))) + facet_grid(Var1~., as.table=FALSE, scale="free_y", space = "free_y") + theme_minimal() + 
    geom_vline(xintercept=max(as.numeric(plot$Var2))+ 0.586, size=0.3) + 
    theme(legend.position="none", 
     axis.text.x = element_text(angle = 90, colour="black", vjust = 0.4, size=8), 
     axis.title.x = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), 
     axis.line.y=element_blank(), axis.text.y=element_blank(), 
     strip.text.y=element_text(size = 8, colour="black", family="",angle=00, hjust = 0), 
     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")) 
bars 

## exporting barplot "plot.png" to directory 

#loading Cairo 
if (!require("Cairo")) { 
    install.packages("Cairo", repos="http://cran.rstudio.com/") 
    library("Cairo") 
} 
library("Cairo") 

#preparing exporting 
png(file="plot.png",type="cairo", width = 4, height = 4.2, units = 'in',pointsize=8,res=600) 

#exporting 
barplot ; dev.off() 

結果:

enter image description here

這例如「a」總分爲0.81(最大爲1),但分爲0.2(紅色),0,0.6(淺藍色)和0.19(深藍色)子列。 的事情是,我想y軸添加到每個子欄節包含此信息,這樣其他的影像中:

enter image description here

你知道ggplot,可以做任何選項這個?

回答

1

您明確地關閉了Y軸爲你的板帶:

theme(..., axis.title.y = element_blank(), axis.ticks.y = element_blank(), axis.line.y=element_blank(), axis.text.y=element_blank(), ...)

刪除那些線條,以及你希望你的身材應該會出現。

+0

OMG你是如此的正確,我必須刪除他們出於某種原因,然後忘了它...謝謝你注意到它! – Jaime