R 中箱线图上的轴标签问题
Issues with axis labeling on boxplots in R
希望这是一个快速修复。我正在尝试使用以前担任我职位的人编写的 R 代码制作营养河流浓度的箱线图(我对 R 的经验不多,但我们仅将其用于此目的)。问题是输出箱线图轴有多个重叠文本,其中一些似乎来自代码的另一部分,我认为它没有指定轴标签。原始代码如下所示(工作目录已设置,csv 文件已导入,我知道可以工作),生成的箱线图位于 1.
编辑:代码如下
png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
width=10,
height=4,
unit="in",
res=600)
par(mar=c(5,5,3,1),
cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist,
ylim=c(0,3000),
xaxt="n"),
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
-50,
paste("n=",
tnhistconc$n),
cex=0.8)
title(ylab="TN Concentration (ppb)",
xlab="Year")
title(main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(TNhist$Year),") TN Concentration"))
dev.off()
我编辑了在 xlab="Year" 之后向底部添加 ,xlab="",ylab="" 的编辑,因为这在箱线图代码的其他类似部分中解决了这个问题(除了我似乎需要将其添加到这些部分的不同部分,请参阅 2 - also tried it after xaxt ="n" as in 2 and got the same result). It fixes the overlapping text issue, but the axis labels are still not what I want them to be ("Year", and "TN Concentration (ppb)), and this is shown in 3。
那么,有没有人可能知道一个简单的修复方法可以去除这些不需要的标签并用正确的标签替换它们?我错过了一些基本的东西吗?在我这样做之前,相同的原始代码在过去似乎运行良好(对于 2018 年的数据),并且从中导入数据的电子表格是相同的,相同的设置和所有内容。提前谢谢了!
编辑:我有一个示例数据集,它只是最近 2 年的数据。看这里:https://docs.google.com/spreadsheets/d/10oo9w-IzXkLWdY10A9gHYhDH67MeSibBpc2q67L6o88/edit?usp=sharing
Original code result
How this code fixed other similar issues
Partially fixed result based on edit
不知道是不是打错了,你在xatx = "n"
后面多了一个括号。
也许你可以尝试这样的事情:
png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
width=10, height=4, unit="in", res=600)
par(mar=c(5,5,3,1), cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010], data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist,
ylim=c(0,3000),
xaxt="n", ylab = "", xlab = "",
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
-50,
paste("n=",
tnhistconc$n),
cex=0.8)
title(ylab="TN Concentration (ppb)",
xlab="Year",
main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(TNhist$Year),") TN Concentration"))
dev.off()
xatx
将删除 x 轴(由 axis.break
控制)。xlab
和 ylab
将删除 x 和 y 轴标题,它们将在稍后设置通过 title
.
希望它能奏效
编辑:使用ggplot2
您的数据框实际上是更长的格式,可以很容易地在几行中使用 ggplot2
进行绘制。这里你的数据集被命名为 df
:
library(ggplot2)
ggplot(df, aes(x = as.factor(Year), y = Conc_ppb))+
geom_boxplot()+
labs(x = "Year", y = "TN Concentration (ppb)",
title = paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(df$Year),") TN Concentration"))
希望这是一个快速修复。我正在尝试使用以前担任我职位的人编写的 R 代码制作营养河流浓度的箱线图(我对 R 的经验不多,但我们仅将其用于此目的)。问题是输出箱线图轴有多个重叠文本,其中一些似乎来自代码的另一部分,我认为它没有指定轴标签。原始代码如下所示(工作目录已设置,csv 文件已导入,我知道可以工作),生成的箱线图位于 1.
编辑:代码如下
png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
width=10,
height=4,
unit="in",
res=600)
par(mar=c(5,5,3,1),
cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist,
ylim=c(0,3000),
xaxt="n"),
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
-50,
paste("n=",
tnhistconc$n),
cex=0.8)
title(ylab="TN Concentration (ppb)",
xlab="Year")
title(main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(TNhist$Year),") TN Concentration"))
dev.off()
我编辑了在 xlab="Year" 之后向底部添加 ,xlab="",ylab="" 的编辑,因为这在箱线图代码的其他类似部分中解决了这个问题(除了我似乎需要将其添加到这些部分的不同部分,请参阅 2 - also tried it after xaxt ="n" as in 2 and got the same result). It fixes the overlapping text issue, but the axis labels are still not what I want them to be ("Year", and "TN Concentration (ppb)), and this is shown in 3。
那么,有没有人可能知道一个简单的修复方法可以去除这些不需要的标签并用正确的标签替换它们?我错过了一些基本的东西吗?在我这样做之前,相同的原始代码在过去似乎运行良好(对于 2018 年的数据),并且从中导入数据的电子表格是相同的,相同的设置和所有内容。提前谢谢了!
编辑:我有一个示例数据集,它只是最近 2 年的数据。看这里:https://docs.google.com/spreadsheets/d/10oo9w-IzXkLWdY10A9gHYhDH67MeSibBpc2q67L6o88/edit?usp=sharing
Original code result
How this code fixed other similar issues
Partially fixed result based on edit
不知道是不是打错了,你在xatx = "n"
后面多了一个括号。
也许你可以尝试这样的事情:
png(filename="./TP & TN Plotting/Plots/TN Concentration/Historical TN Concentrations Zoomed to Medians.png",
width=10, height=4, unit="in", res=600)
par(mar=c(5,5,3,1), cex=.75)
tnhistconc<-(boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010], data=TNhist))
boxplot(Conc_ppb[Year!=2009 & Year !=2010]~Year[Year!=2009 & Year !=2010],
data=TNhist,
ylim=c(0,3000),
xaxt="n", ylab = "", xlab = "",
at=c(1:3,5:8,10:(length(tnhistconc$n)+2)))
axis.break(axis=1,breakpos=c(4),style="slash")
axis.break(axis=1,breakpos=c(9),style="slash")
text(c(1:3,5:8,10:(length(tnhistconc$n)+2)),
-50,
paste("n=",
tnhistconc$n),
cex=0.8)
title(ylab="TN Concentration (ppb)",
xlab="Year",
main=paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(TNhist$Year),") TN Concentration"))
dev.off()
xatx
将删除 x 轴(由 axis.break
控制)。xlab
和 ylab
将删除 x 和 y 轴标题,它们将在稍后设置通过 title
.
希望它能奏效
编辑:使用ggplot2
您的数据框实际上是更长的格式,可以很容易地在几行中使用 ggplot2
进行绘制。这里你的数据集被命名为 df
:
library(ggplot2)
ggplot(df, aes(x = as.factor(Year), y = Conc_ppb))+
geom_boxplot()+
labs(x = "Year", y = "TN Concentration (ppb)",
title = paste("Historical (1998 - 2000), (2005 - 2008) + UMass (2012 -
",max(df$Year),") TN Concentration"))