摆脱直方图中的向下条形

时间:2018-10-26 19:33:34

标签: r ggplot2 histogram

我写了一个小ggplot2,使用R从csv文件生成TikZ直方图。对于连续的y轴刻度,它可以正常工作,但是当我在y轴上使用对数刻度时,它会产生令人不满意的结果。

这是脚本:

 #!/usr/bin/env Rscript

  # for Python-like command-line argument parsing
  #  install.packages(c("optparse"))
  #  install.packages("ggplot2")
  library("optparse")
  library(ggplot2)
  library(reshape)

  # load the tikzDevice package, if you dont have it, install with:
  #  install.packages("tikzDevice", repos="http://R-Forge.R-project.org")
  require(tikzDevice)

  findNextLargerLog <- function(x) 10^ceiling(log10(x))
  findNextSmallerLog <- function(x) 10^floor(log10(x))
  findMax <- function(data) sapply(data, max, na.rm = TRUE)
  findMin <- function(data) sapply(data, min, na.rm = TRUE)

  # read statfile
  dat = read.csv2('test.stat', check.names=FALSE, dec='.')

  # re-extract column names after sorting
  column_names = colnames(dat)

  # melt data so that each row is a unique id-variable combination
  df = melt(dat, id=c(colnames(dat)[1]))

  ymin = findNextSmallerLog(min(df[, 3], na.rm=TRUE))
  ymax = findNextLargerLog(max(df[, 3], na.rm=TRUE))

  # create standalone tikz file with the given width/height
  tikz('test.tex', standAlone=TRUE, width=46, height=34)

  plot = ggplot(data=df, aes(x=as.factor(df[, 1]), y=value, fill=factor(variable), na.rm=TRUE)) + geom_histogram(stat="identity", position="dodge", width=0.75, colour="black") + scale_fill_brewer(palette="Set1", breaks=column_names[-1], labels=column_names[-1]) + guides(fill=guide_legend(nrow=1, keyheight=8, keywidth=8))

  plot = plot + theme_linedraw() + theme(text=element_text(size=180), axis.text.x=element_text(angle=90), panel.grid.major=element_line(linetype="dotted", color="grey65"), panel.grid.minor=element_blank(), legend.position='top', legend.title=element_blank())

  # y axis formatting
  plot = plot + scale_y_log10(breaks=10^(-10:10), minor_breaks=rep(1:9, 21)*(10^rep(-10:10, each=9)), limits=c(ymin, ymax))

  print(plot)

  #close the device
  dev.off()

结果如下:enter image description here

任何建议如何使绿色条向上而不是向下? 我必须承认我是Rgglot2的新手。但是,在搜寻此问题时,我发现没有任何有用的信息。

编辑:用于绘图的数据。

k;A;B;C
1;5002.9400000000005;1361.6975;0.0814965

0 个答案:

没有答案