一个 R 工作室图中的两个对数 y 轴直方图
Two logarithmic y axis histograms in one R studio diagram
我正在使用 RStudio 绘制此 data 中值的直方图。
data = read.table("C:\Test\test.csv", header=TRUE, sep=",")
hist(data$a, breaks=100)
hist(data$b, breaks=100)
并得到以下直方图:
但我想:
1- 记录 y 轴,这样我将使用 0、10、100、1000、10000 等代替值 0、4000、8000 和 12000(log2 即 0、1、2、 4, 8, 16, ... 也很有用)。
2- 将两个图表放在一个图表中(最好使用两个不同 colors/patterns 的条形图)。在生成的图表中,每个 x 值的两个条将像这样并排在一起:
我试过了 solution 但出现以下错误:
NULL
Warning message: In (function () : Only one RStudio graphics
device is permitted
这是我的做法:
## Create fake data
x <- c(rep(1, 100), rep(2, 20000), rep(3, 800), rep(4, 10000))
y <- c(rep(1, 10), rep(2, 1000), rep(3, 10000), rep(4, 2000))
## Plot x
hist.x <- hist(x, plot = FALSE)
hist.x$counts <- log10(hist.x$counts + 1)
plot(hist.x, col = rgb(0, 0, 1, 0.25))
## Plot y
hist.y <- hist(y, plot = FALSE)
hist.y$counts <- log10(hist.y$counts + 1)
plot(hist.y, col = rgb(1, 0, 0, 0.25), add = TRUE)
结果是:
如果您希望它们并排放置,只需添加
par(mfrow = c(1, 2))
在顶部并将 y 的绘图命令更改为 `
绘图(hist.y, col = rgb(1, 0, 0, 0.25))
结果图如下所示:
这是我根据 Teja_K 的回答设计的解决方案:
data = read.table("C:\test\test.csv", header=TRUE, sep=",")
par( mar=c(3.1, 5.1, 0, 0))
hist.x <- hist(data$a, plot = FALSE, breaks=50)
hist.x$counts <- log10(hist.x$counts + 1)
plot(hist.x, col = rgb(0, 0, 1, 0.99), main="", xlab="", ylab="", yaxt="n")
yAxesTitles=c(1, 10, 100, 1000, 10000)
axis(2, at=c(0, 1, 2, 3, 4),labels=yAxesTitles, col.axis="black", las=2)
mtext(side = 1, text = "Number", line = 2)
mtext(side = 2, text = "Frequency", line = 4)
# Adding the second diagram to the first one:
relocatedData=data$b+0.2
hist.y <- hist(relocatedData, plot = FALSE, breaks=50)
hist.y$counts <- log10(hist.y$counts + 1)
plot(hist.y, col = rgb(1, 0, 0, 0.99), main="", xlab="", ylab="", yaxt="n", add=TRUE)
legend(7.5, 4, c("a", "b"), lwd=c(1, 1), col=c(rgb(0, 0, 1, 0.99), rgb(1, 0, 0, 0.99)), pch = c(15, 15), pt.cex=2)
结果:
我正在使用 RStudio 绘制此 data 中值的直方图。
data = read.table("C:\Test\test.csv", header=TRUE, sep=",")
hist(data$a, breaks=100)
hist(data$b, breaks=100)
并得到以下直方图:
1- 记录 y 轴,这样我将使用 0、10、100、1000、10000 等代替值 0、4000、8000 和 12000(log2 即 0、1、2、 4, 8, 16, ... 也很有用)。
2- 将两个图表放在一个图表中(最好使用两个不同 colors/patterns 的条形图)。在生成的图表中,每个 x 值的两个条将像这样并排在一起:
我试过了 solution 但出现以下错误:
NULL
Warning message: In (function () : Only one RStudio graphics
device is permitted
这是我的做法:
## Create fake data
x <- c(rep(1, 100), rep(2, 20000), rep(3, 800), rep(4, 10000))
y <- c(rep(1, 10), rep(2, 1000), rep(3, 10000), rep(4, 2000))
## Plot x
hist.x <- hist(x, plot = FALSE)
hist.x$counts <- log10(hist.x$counts + 1)
plot(hist.x, col = rgb(0, 0, 1, 0.25))
## Plot y
hist.y <- hist(y, plot = FALSE)
hist.y$counts <- log10(hist.y$counts + 1)
plot(hist.y, col = rgb(1, 0, 0, 0.25), add = TRUE)
结果是:
如果您希望它们并排放置,只需添加
par(mfrow = c(1, 2))
在顶部并将 y 的绘图命令更改为 `
绘图(hist.y, col = rgb(1, 0, 0, 0.25))
结果图如下所示:
这是我根据 Teja_K 的回答设计的解决方案:
data = read.table("C:\test\test.csv", header=TRUE, sep=",")
par( mar=c(3.1, 5.1, 0, 0))
hist.x <- hist(data$a, plot = FALSE, breaks=50)
hist.x$counts <- log10(hist.x$counts + 1)
plot(hist.x, col = rgb(0, 0, 1, 0.99), main="", xlab="", ylab="", yaxt="n")
yAxesTitles=c(1, 10, 100, 1000, 10000)
axis(2, at=c(0, 1, 2, 3, 4),labels=yAxesTitles, col.axis="black", las=2)
mtext(side = 1, text = "Number", line = 2)
mtext(side = 2, text = "Frequency", line = 4)
# Adding the second diagram to the first one:
relocatedData=data$b+0.2
hist.y <- hist(relocatedData, plot = FALSE, breaks=50)
hist.y$counts <- log10(hist.y$counts + 1)
plot(hist.y, col = rgb(1, 0, 0, 0.99), main="", xlab="", ylab="", yaxt="n", add=TRUE)
legend(7.5, 4, c("a", "b"), lwd=c(1, 1), col=c(rgb(0, 0, 1, 0.99), rgb(1, 0, 0, 0.99)), pch = c(15, 15), pt.cex=2)
结果: