Я написал небольшой ggplot2
для генерации гистограмм TikZ из CSV-файлов, используя R
. Для непрерывного масштабирования по оси y он работает нормально, однако он дает неудовлетворительные результаты, когда я использую logscale на оси 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()
Результат выглядит следующим образом: 
Есть предложения, как заставить зеленую полосу идти вверх, а не вниз?
Я должен признать, что я новичок в R
и gglot2
. Однако я не нашел ничего полезного при поиске этой проблемы.
РЕДАКТИРОВАТЬ: данные, используемые для построения.
k;A;B;C
1;5002.9400000000005;1361.6975;0.0814965