R 箱线图中的标记语法
Labeling Syntax in R Boxplot
尝试在箱线图上建立 "range" 标签:
x <- getURL('https://raw.githubusercontent.com/dothemathonthatone/maps/master/maindf_2_Anon.csv')
maindf <- read.csv(text = x, row.names=NULL, head =TRUE, dec = ',')
maindf$fee_per_inc <- as.numeric(as.character(maindf$fee_per_inc))
summary(maindf$year_hh_inc)
Min. 1st Qu. Median Mean 3rd Qu. Max.
7501 35000 50001 56446 75001 500014
maindf <- maindf %>%
mutate(category = cut(year_hh_inc,
breaks = (quantile(year_hh_inc, c(0, 1/4, 2/4, 3/4, 1), na.rm = TRUE)),
labels = c("€ 7,501 - € 35,000", "35,001 - 50,001", "50,002 - 75,001", "75,002 - 500,001"),
include.lowest = TRUE),
vals = 1)
maindf <- maindf[maindf$fee_per_inc > 0 & maindf$fee_per_inc < 0.0001, ]
box <- boxplot(maindf$fee_per_inc ~ maindf$category, col = 3:5)
我想用 summary
调用的范围标记每个框。由于某种原因,它只拿起两个盒子。有人对此有想法吗?
我当然希望可以对这些颜色做些什么?
标签已经是 "there",但是 R 的原生 plot 方法会在轴标签发生冲突时丢弃轴标签,因此您只需要缩小轴文本即可:
boxplot(maindf$fee_per_inc ~ maindf$category, col = 3:5, cex.axis = 0.9)
标签没有完整显示的原因很简单:它们太长了。解决方案也很简单:只需将三个参数调整为 boxplot
:
首先,increase/decrease 箱线图周围的边距,因此通过调整 mar
中的值,您可以为宽敞的标签提供更多 space;例如,您使 space 在 下的箱线图足够大,以便标签适合在:
par(mfrow = c(1,1), mar = c(8,4,1,4))
Then/Or 通过减小 cex.axis
调整轴标签的字符大小(默认为 1)
最后您可以设置las = 2
翻转标签的方向。
您还提到颜色有问题:您可以通过在参数 col
:
中定义您选择的颜色,以任何您想要的方式轻松更改它们
boxplot(women$height, women$weight,
names = c("A long variable name", "B another long name"),
cex.axis = 0.8,
las = 2,
col = c("red", "blue"))
尝试在箱线图上建立 "range" 标签:
x <- getURL('https://raw.githubusercontent.com/dothemathonthatone/maps/master/maindf_2_Anon.csv')
maindf <- read.csv(text = x, row.names=NULL, head =TRUE, dec = ',')
maindf$fee_per_inc <- as.numeric(as.character(maindf$fee_per_inc))
summary(maindf$year_hh_inc)
Min. 1st Qu. Median Mean 3rd Qu. Max.
7501 35000 50001 56446 75001 500014
maindf <- maindf %>%
mutate(category = cut(year_hh_inc,
breaks = (quantile(year_hh_inc, c(0, 1/4, 2/4, 3/4, 1), na.rm = TRUE)),
labels = c("€ 7,501 - € 35,000", "35,001 - 50,001", "50,002 - 75,001", "75,002 - 500,001"),
include.lowest = TRUE),
vals = 1)
maindf <- maindf[maindf$fee_per_inc > 0 & maindf$fee_per_inc < 0.0001, ]
box <- boxplot(maindf$fee_per_inc ~ maindf$category, col = 3:5)
我想用 summary
调用的范围标记每个框。由于某种原因,它只拿起两个盒子。有人对此有想法吗?
我当然希望可以对这些颜色做些什么?
标签已经是 "there",但是 R 的原生 plot 方法会在轴标签发生冲突时丢弃轴标签,因此您只需要缩小轴文本即可:
boxplot(maindf$fee_per_inc ~ maindf$category, col = 3:5, cex.axis = 0.9)
标签没有完整显示的原因很简单:它们太长了。解决方案也很简单:只需将三个参数调整为 boxplot
:
首先,increase/decrease 箱线图周围的边距,因此通过调整 mar
中的值,您可以为宽敞的标签提供更多 space;例如,您使 space 在 下的箱线图足够大,以便标签适合在:
par(mfrow = c(1,1), mar = c(8,4,1,4))
Then/Or 通过减小 cex.axis
调整轴标签的字符大小(默认为 1)
最后您可以设置las = 2
翻转标签的方向。
您还提到颜色有问题:您可以通过在参数 col
:
boxplot(women$height, women$weight,
names = c("A long variable name", "B another long name"),
cex.axis = 0.8,
las = 2,
col = c("red", "blue"))