使用ggplot

时间:2017-05-25 17:10:57

标签: r ggplot2 bar-chart

我有以下数据(4列和6行数据):

    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

我已经提出了这个R脚本,它使用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 个答案:

答案 0 :(得分:1)

您明确关闭了面板的y轴:

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

删除这些行,你的数字应该按照你的意愿出现。